码迷,mamicode.com
首页 > 其他好文 > 详细

cgo

时间:2019-02-02 14:15:06      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:pointer   str   not   strong   export   dir   idf   ror   stat   

Go references to C
1、Memory allocations made by C code are not known to Go‘s memory manager. When you create a C string with C.CString (or any C memory allocation) you must remember to free the memory when you‘re done with it by calling C.free.

2 、To access a struct, union, or enum type directly, prefix it with struct_, union_, or enum_, as in C.struct_stat.

3、The size of any C type T is available as C.sizeof_T, as in C.sizeof_struct_stat.

4、_, err := C.voidFunc()  C errno variable as an error (use _ to skip the result value if the function returns void).

C references to Go 
1、go函数导出,单个返回值与多返回值。found in the _cgo_export.h generated header, after any preambles copied from the cgo input files. Functions with multiple return values are mapped to functions returning a struct.
//export MyFunctionf
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}

 


//export MyFunction2f
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
they will be available in the C code as:
extern int64 MyFunction(int arg1, int arg2, GoString arg3);

extern struct MyFunction2_return MyFunction2(int arg1, int arg2, GoString arg3);

2 、Go code may not store a Go pointer in C memory. C code may store Go pointers in C memory, subject to the rule above: it must stop storing the Go pointer when the C function returns.

  

cgo

标签:pointer   str   not   strong   export   dir   idf   ror   stat   

原文地址:https://www.cnblogs.com/xiaomayi-cyj/p/10348279.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!