參考文章:
http://golang.org/cmd/cgo is the primary cgo documentation
There is also a good introduction article at http://golang.org/doc/articles/c_go_cgo.html.
example:https://github.com/golang/go/wiki/cgo
數(shù)據(jù)類型轉(zhuǎn)換
1.C -> Go:
int(C.int )
// C string to Go string
func C.GoString(*C.char) string
// C string, length to Go string
func C.GoStringN(*C.char, C.int) string
// C pointer, length to Go []byte
func C.GoBytes(unsafe.Pointer, C.int) []byte
2.Go -> C:
C.char
C.schar (signed char)
C.uchar (unsigned char)
C.short
C.ushort (unsigned short)
C.int
C.uint (unsigned int)
C.long
C.ulong (unsigned long)
C.longlong (long long)
C.ulonglong (unsigned long long)
C.float
C.double.
unsafe.Pointer (void*)
// Go string to C string
func C.CString(string) *C.char
var val []byte
(*C.char)(unsafe.Pointer(&val[0]))