C structure:
typedef struct info_s {
int len;
uint8_t *num;
}info_t;
extern int info_collect(int unit, info_t *info,
data_t *data);
Go Wrapper:
type Info struct {
Len int
num []uint8
}
//Method to convert C.info_t => Info
func (inf C.info_s) Info() Info {
var tInf Info
tInf.Len = int(inf.len)
for i := 0; i < int(tInf.Len); i++ {
tInf.num[i] = uint8(inf.num[i])
}
return Info{
Len: int(inf.len),
Info: (*C.uchar)(unsafe.Pointer(&info.num[0])),
}
}
How do I access the uint8_t *num
from go wrapper?
I don't think the method defined is correct.
Is the struct for num - (num []uint8
) the correct way to access this?
Adding a missing piece of the puzzle. There is an API in C as well that takes the C struct as an input.
Now when I call this API, I get a runtime panic:
data := []uint8{1, 2, 3}
var inf = new(C.info_t)
inf.len = 64
inf.num = data
C.info_collect(C.int(unit), (*C.info_t)(unsafe.Pointer(&info)),
(*C.data_t)(unsafe.Pointer(&data)))
Panic: runtime error: cgo argument has Go pointer to Go pointer