CGo Passing pointer to a cstring into a c callback

// Init C string
str := C.Cstring(“foo”)

// Call C function with callback func- callback(void *) and str
C.cfun(str, C.callback) // cfun(void *data)

// C.callback calls goCallback
callback(void * data){
// able to fetch str at this point but not inside goCallback() function.
goCallback(data);
}

What could be the reason?

You must call C.GoString or C.GoStringN in the Go callback to convert to a Go string. From cgo command - cmd/cgo - pkg.go.dev

// C string to Go string
func C.GoString(*C.char) string

// C data with explicit length to Go string
func C.GoStringN(*C.char, C.int) string

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.