I'm trying to call a C library from GO using cgo.
The C library has the following function:
int receive(void** data);
// I'd call it like that:
void* myptr; // myptr=null
int nbBytes = receive(&myptr);
if (nbBytes==0) { return }
// myptr has now an address to a valid buffer that contains nbBytes bytes.
// And then casting it with char* to access the data that can be anything. Example:
printf("%d", *(char*)myptr);
How can I call this receive()
function from GO? Go doesn't allocate any memory, the memory address is returned via myptr
and directly access from it.
receive()
is a "no-copy" and writes the actual data's address intomyptr
. The data is then accessed using*(char*)myptr
.- we can assume
receive()
allocates and frees the buffer, it's hidden from the lib's user
Ideally, I would read the data via []byte
in go.