简体   繁体   English

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

[英]How to pass a Go function as an argument to a C function?

I'm trying to pass a Go function to a C function. 我正在尝试将Go函数传递给C函数。

Something like: 就像是:

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

and pass that into a C function: 并将其传递给C函数:

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

I'd just like to know how to define something like C.function. 我只想知道如何定义类似C.function的东西。

the main problem is misunderstanding of definition of go in c. 主要问题是对c中go定义的误解。 so final code is look like 所以最终的代码看起来像


//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

...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM