After seeing how slices work, we will know how to handle complex data in C and Go using structures. For this, let's see the following sections.
Working with structs
Structures in Go
Go structures use a technique called alignment, which consists of adding one or more bytes to data structures to make it fit into memory addresses better. Consider the following data structure:
struct {
a string
b bool
c []byte
}
With 64-bit architecture calling unsafe.Sizeof on this structure, this will give us an unexpected result. What we are expecting is the following:
- 16 bytes from the string; 8 for the pointer to the first element, and 8 for the length
- 1 byte for the Boolean
- 24 for the slice; 8 for the address, 8 for...