cgo: C的struct转换为go unsafe.Pointer的办法

cgo: C的struct转换为go unsafe.Pointer的办法

如果go build时报错:
cannot use _cgo5 (type _Ctype_struct__xx) as type unsafe.Pointer in argument.
例如C函数中函数声明是int SetUserData(void * userData), 其中userData为一个struct,
可用这个办法将C的struct转换为go unsafe.Pointer:

var a C.UserData
udata := unsafe.Pointer(&a)
C.SetUserData(udata)

你可能感兴趣的:(golang,golang,cgo)