如何将 Go 函数作为参数传递给 C 函数?

How to pass a Go function as an argument to a C function?

我正在尝试将 Go 函数传递给 C 函数。

类似于:

stm := C.struct_tray_menu{
    ....
    fn: // definition of method
    ....
}
C.menu_cb(stm);

并将其传递给 C 函数:

static void menu_cb(struct tray_menu *item) {
  (void)item;
  printf("menu: clicked on %s\n", item->text);
}

我只想知道如何定义类似 C.function 的内容。

主要问题是对c中go定义的误解。 所以最终代码看起来像


//export callOnMeGo
func callOnMeGo(in int) int {
    fmt.Printf("Go.callOnMeGo(): called with arg = %d\n", in)
    return  in+ 1
}

func main() {

    C.some_c_func((C.callback_fcn)(unsafe.Pointer(C.callOnMeGo_cgo)))
    //dont forget to use (funcDefinedInGO_cgo) for with postfix _cgo

...