Hi,

You cannot pass f, which is a Go function, in a CGo call directly because 
of the pointer passing rules. You need to create some wrappers, and I have 
found this article 
(https://eli.thegreenplace.net/2019/passing-callbacks-and-pointers-to-cgo) 
very helpful. 

That being said, I have a feeling that if you provide some more details 
about what you are trying to achieve, people here might be able to help you 
get a simpler solution in Go.

Tzu-Yu
ulis lee 在 2023年11月22日 星期三上午11:28:20 [UTC+8] 的信中寫道:

> *Hi, all*
>
> It's been a month since I started Golang.
> But I was in a difficult situation. I need your help.
>
> *C code*
>
> int reopen (const char *fn, time_t check_interval_sec, int 
> (*callback_func) (int num_entry, char **entry), int retry);
>
>
> *Go code*
>
> func _Open(fileName string, intervalSec int, f func(C.int, **C.char) 
> C.int) int {
>
>    cFileName := C.CString(fileName)
>
>    defer C.free(unsafe.Pointer(cFileName))
>
>
>     ret := C.reopen(cFileName, C.long(intervalSec), f, 1)
>
>     return int(ret)
>
> }
>
> error message 
> cannot use f (variable of type func(_Ctype_int, **_Ctype_char) _Ctype_int) 
> as *[0]byte value in argument to (_Cfunc_reopen)
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2b58349f-4d8f-44c9-805b-f7b52f92bbedn%40googlegroups.com.

Reply via email to