我在C中有以下函数set
void set(void *data)
{
// do some work with data (which points to a Go variable)
}现在,在Go中,我需要有一个接受任何变量作为参数的函数,并调用C函数set,将此参数传递给它。我该怎么做呢?我们是否可以这样做:让Go函数接受像data interface{}这样的参数,调用传递此参数的unsafe.Pointer,然后使用unsafe.Pointer的结果调用C函数
发布于 2021-09-14 19:08:55
只要您遵循CGO文档的passing pointers部分中详细描述的约束,就可以创建一个接受空接口参数的Go包装器函数,并使用reflect提取指针值。
func Set(i interface{}) {
C.set(unsafe.Pointer(reflect.ValueOf(i).Pointer()))
}https://stackoverflow.com/questions/69179965
复制相似问题