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
In Go, num
is simply a slice of type []uint8
(or its alias []byte
) of length len(num)
. To copy data from C type info_t
to Go managed memory type []uint8
, write a Go num
function:
package main
import (
"fmt"
"unsafe"
)
/*
#include <stdint.h>
typedef struct info_s {
int len;
uint8_t *num;
} info_t;
*/
import "C"
func num(info C.info_t) []uint8 {
n := make([]uint8, 0, info.len)
for i := _Ctype_int(0); i < info.len; i++ {
u8 := *(*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(info.num)) + uintptr(i)))
n = append(n, u8)
}
return n
}
func main() {
test := []uint8{1, 2, 3}
fmt.Println(len(test), test)
info := C.info_t{
len: _Ctype_int(len(test)),
num: (*_Ctype_uint8_t)(&test[0]),
}
infonum := (*[1 << 20]uint8)(unsafe.Pointer(info.num))[:info.len]
fmt.Println(info.len, infonum)
n := num(info)
fmt.Println(len(n), n)
}
Output:
3 [1 2 3]
3 [1 2 3]
3 [1 2 3]