cgo是否支持回调

wenjin_gu · · 4098 次点击
polaris
社区,需要你我一同完善!
Go暴露给 C 的函数,没法直接在 Go 中转为 C 的类型,只能在 C 中使用它。
#5
更多评论
polaris
社区,需要你我一同完善!
说的是这样吗? package main // typedef int (*intFunc) (); // // int // bridge_int_func(intFunc f) // { // return f(); // } // // int fortytwo() // { // return 42; // } import "C" import "fmt" func main() { f := C.intFunc(C.fortytwo) fmt.Println(int(C.bridge_int_func(f))) // Output: 42 }
#1
首先谢谢您的回答。我主要是想用Go语言来实现回调函数,对应到您的代码就是将fortytwo函数用Go语言来实现。由于cgo是不支持在C代码中调用Go代码的,所以这种方式恐怕不行。
#2