cgo

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.

  

posted @ 2019-02-02 13:38  小蚂蚁_CYJ  阅读(376)  评论(0编辑  收藏  举报