Best way to pass slice of slices in CGO?

  Kiến thức lập trình

I have a static C++ library that will be called from Go using CGO. In one of my functions, I want to pass a serialised object (and perform deserialisation in C/C++).

This serialised object is represented in Go as []byte.

I want to pass multiple of these objects per CGO call, effectively then passing [][]byte.

When writing C++, my usual method of passing matrices to a function would be to have a pointer to a pointer in the argument. With arguments to CGO calls that are slices, I am passing the size + pointer to the first element. I’m not sure however how I can do that with CGO.

Would

(**C.uint8_t)(&myslice[0][0])

Work? By work I mean I will be able to access elements of the slice individually inside C/C++ by doing e.g mySlice[i][j]. But I’m not even sure this casting makes sense (I am getting a go address and casting to a C pointer to pointer).

Open also to suggestions on better ways to do this.

LEAVE A COMMENT