I have a byte.Buffer that I pack with data using the binary.Write() function. I then need to send this byte array to a C function. Using Go 1.6 I have not been successful at figuring this out.
buf := new(bytes.Buffer) //create my buffer
....
binary.Write(buf, binary.LittleEndian, data) //write my data to buffer here
addr := (*C.uchar)(unsafe.Pointer(&buf.Bytes()[0])) //convert buffers byte array to a C array
rc := C.the_function(addr, C.int(buf.Len())) //Fails here
It fails on the line calling the C function saying:
panic: runtime error: cgo argument has Go pointer to Go pointer
The C function:
int the_function(const void *data, int nbytes);
I was able to get the following to work, but it felt wrong converting the byte array to a string. Is there a better way to do this? Does this method risk side effects to the data?
addr := unsafe.Pointer(C.CString(string(buf.Bytes()[0]))
Again this needs to work under Go 1.6 which introduced stricter cgo pointer rules.
Thank you.