Unpacked structures

Each struct we define will be unpacked unless otherwise specified. We can define a structure in C as follows:

typedef struct{
unsigned char a;
char b;
int c;
unsigned int d;
char e[10];
} myStruct;

We can use it and populate it with values directly from our Go code without any issues arising:

func main() {
v := C.myStruct{
a: C.uchar('A'),
b: C.char('Z'),
c: C.int(100),
d: C.uint(10),
e: [10]C.char{'h', 'e', 'l', 'l', 'o'},
}
log.Printf("%#v", v)
}

This small test will give us the following output:

main._Ctype_struct___0{
a:0x41,
b:90,
c:100,
d:0xa,
e:[10]main._Ctype_char{104, 101, 108, 108, 111, 0, 0, 0, 0, 0},
_:[2]uint8{0x0, 0x0},
}

This tells us that there is an extra blank field that is used for padding because the last field is 10 bytes, which is 2 bytes short of being a multiple of 4 (that is, 12 bytes).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.66.156