【发布时间】:2015-10-05 19:54:23
【问题描述】:
我在 C lib 中有一个结构,其中定义了一些回调。 Go 将此字段视为*[0]byte 数组类型并且我无法将其设置为指针的问题:
./test.go:16: cannot use _Cgo_ptr(_Cfpvar_fp_cb_func) (type unsafe.Pointer) as type *[0]byte in assignment
问题代码示例:
package main
/*
void cb_func();
typedef struct cb_s {
void (*cb_f)();
} cb_s;
*/
import "C"
//export cb_func
func cb_func() {}
func main() {
var x C.struct_cb_s
// here I want to set callback cb_f to pointer of cb_func().
x.cb_f = C.cb_func
}
一种可能的解决方案 - 编写一个 C 设置器,如下所示:
void cb_set(cb_s *s) {
s->cb_f = &cb_func;
}
但它看起来很难看:我不能将 cb_func 作为参数传递给 setter(已经尝试过 cb_set(cb_s *s, void(*func)()),但在 *[0]byte 上遇到了同样的错误)并且有许多类似的回调,因此需要为每个回调编写 setter一对回调——回调函数。
还有其他解决方案吗?
【问题讨论】: