I have this C code:
uint8_t *data[BUF_SIZE];
data = ...;
// extern void goReadData(uint8_t *data, int bufferSize);
goReadData(data, BUF_SIZE)
And in the GO Code I'm trying to use the data
pointer as a GO array or slice, I want to retrieve an []uint8 from the *C.uint8_t. I know the size of the data
//export goReadData
func goReadData(data *C.uint8_t, bufferSize C.int) {
fmt.Printf("Data type %v\n", reflect.TypeOf(data))
// print 1: Data type *main._Ctype_uchar
// Solution 1: GoBytes
// works but really slow (memory copy I think)
goBytes := C.GoBytes(unsafe.Pointer(data), bufferSize)
fmt.Printf("goBytes type %v\n", reflect.TypeOf(goBytes))
// print 2: goBytes type []uint8
// Solution 2: direct pointer
// Really fast, but wrong type at the end
// unsafe.Pointer to the C array
unsafePtr := unsafe.Pointer(data)
// convert unsafePtr to a pointer of the type *[1 << 30]C.uint8_t
arrayPtr := (*[1 << 30]C.uint8_t)(unsafePtr)
// slice the array into a Go slice, with the same backing array
// as data, making sure to specify the capacity as well as
// the length.
length := int(bufferSize)
slice := arrayPtr[0:length:length]
fmt.Printf("Direct slice type %v\n", reflect.TypeOf(slice))
//Print 3: Direct type []main._Ctype_uchar
}
How could I do to recover an []uint8
instead of []main._Ctype_uchar with the second solution? Or do you have another solution to do that without a bytes copy?
Sorry guys, I found my own mistake:
// convert unsafePtr to a pointer of the type *[1 << 30]C.uint8_t
arrayPtr := (*[1 << 30]C.uint8_t)(unsafePtr)
==> to
// convert unsafePtr to a pointer of the type *[1 << 30]uint8
arrayPtr := (*[1 << 30]uint8)(unsafePtr)
Problem solved!
Thanks ;)