How memory works internally

When you have a struct like this one:

type myStruct struct {
 	myInt   bool    // 1 byte
	myFloat float64 // 8 bytes
	myBool  int32   // 4 bytes
}

As you see a boolean takes 1 byte, a float64 8 bytes, and an int32 4 bytes.

But the memory allocates consecutive packet of 8 bytes. So instead of taking 1 + 8 + 4 = 13 bytes. This struct will takes : 24 bytes

a := myStruct{}
fmt.Println(unsafe.Sizeof(a)) // 24 bytes

How to optimize : It’s possible to optimize, ordering the struct by bytes taken:

type myStructOptimized struct {
 	myFloat float64 // 8 bytes
	myInt   int32   // 4 bytes
	myBool  bool    // 1 byte
}

This new struct ordered will take now:

b := myStructOptimized{}
fmt.Println(unsafe.Sizeof(b)) // ordered 16 bytes

16 bytes, because in memory it will be allocated like that (https://play.golang.org/p/gmkrt6X7aM) :

8 + 8 = 16 bytes