Padding in Go structs
I recently came across a simple but surprisingly rich concept in Go: padding. It is the space the compiler inserts between fields so each value aligns with the architecture’s expectations. Once you see how much wasted room can live between the fields, you start to appreciate how a small reordering can shrink both the in-memory size of a struct and the amount of memory you move around. Why padding exists at all Go lays out struct fields according to each type’s alignment requirement. The compiler inserts padding bytes between fields so the next field starts at the proper boundary. This alignment keeps the CPU happy but can leave gaps between fields or push later fields forward so the entire struct ends up bigger than the sum of its fields. ...