Breaking Down Golang Struct Size

Mar 27, 2025


Explaining Golang Struct Size

A few days ago, I had an interview for a Golang developer position. At the start, I was given a 10 minute multiple choice test. One of the questions involved calculating the byte size of a struct in Golang. I was asked to determine the total number of bytes for a struct containing two data types, a boolean and an integer, in a 64-bit system, without considering memory alignment and padding.

Easy, right? You just need to add the byte sizes of the integer and the boolean since we're ignoring memory alignment and padding. On a 64 bit system, an integer is 8 bytes, and a boolean is 1 byte. In total, the struct is 9 bytes in size.

But you might be wondering, what is this memory alignment and padding that I keep mentioning?


Memory Alignment and Padding in Golang

Memory alignment ensures data is stored at addresses that make CPU access faster. Each type has a natural alignment, meaning it works best when stored at memory locations that match its size. For example, on a 64 bit system, an int64 (8 bytes) should be placed at an address that is a multiple of 8. If it's misaligned, the CPU may take extra steps to read it, slowing down the program. To avoid this, Go adds padding between struct fields.

Consider this struct:

type Example struct {
a bool // 1 byte
b int // 8 bytes
c bool // 1 byte
}
type Example struct {
a bool // 1 byte
b int // 8 bytes
c bool // 1 byte
}

While it seems like it should take 10 bytes, Go adds 7 bytes of padding after the boolean types to align to an 8 byte boundary. This makes the total size 24 bytes. If it's still unclear, let me help you visualize the memory allocation.

Unoptimized Struct Memory Allocation

Unoptimized Struct Memory Allocation

If we count the number of blue and pink boxes in the image, the total is 24 because each boolean in the struct rounds up to 8 bytes by adding 7 bytes of padding when sequenced this way.

What if we rearrange the fields like so?

type Example struct {
b int // 8 bytes
a bool // 1 byte
c bool // 1 byte
}
type Example struct {
b int // 8 bytes
a bool // 1 byte
c bool // 1 byte
}

Let me illustrate how the memory allocation process works under the hood with this simple adjustment we made.

Optimized Struct Memory Allocation

Optimized Struct Memory Allocation

Here, the integer is already aligned at the start of the struct, so no extra padding is needed. The two booleans follow immediately after. In the unoptimized struct, each boolean adds 7 bytes of padding to align to 8 bytes. However, in the optimized struct, the two booleans together take up 2 bytes, so only 6 bytes of padding are needed to reach the next 8-byte boundary. As long as small fields like booleans are grouped together and their total size is less than 8 bytes, Go combines them before adding padding, rather than padding each one separately. Now, if we add up all the pink and blue boxes, the total comes to 16 boxes or 16 bytes.

If you want to test and verify this in Golang, you can use the following code to check the final struct size.

package main

import (
"fmt"
"unsafe"
)

type Example struct {
a bool
b int
c bool
}

func main() {
fmt.Println(unsafe.Sizeof(Example{}))
}

package main

import (
"fmt"
"unsafe"
)

type Example struct {
a bool
b int
c bool
}

func main() {
fmt.Println(unsafe.Sizeof(Example{}))
}


Conclusion

In most real-world applications, struct optimization usually doesn’t make a noticeable difference. For typical web applications or backend services, the impact of padding is negligible because modern systems have plenty of memory, and the CPU handles alignment efficiently. However, in performance-critical applications like low-level systems programming, high-frequency trading, game development, or embedded systems, reducing struct size can improve memory efficiency and cache performance.

This article is part of my journey in learning Golang, inspired by a real interview experience. By documenting my learning process, hopefully, it helps improving my understanding while also sharing insights that might benefit others.