Golang / Golang Internals and Memory Management Interview Questions
How does struct field ordering affect memory layout and performance in Go?
CPU architectures require data to be aligned — an 8-byte integer must start at an address divisible by 8, a 4-byte integer divisible by 4, etc. The Go compiler adds invisible padding bytes between struct fields to satisfy alignment requirements. Poor field ordering wastes memory; reordering fields can eliminate padding.
// Poorly ordered — wastes 7 bytes of padding
type Wasteful struct {
a bool // 1 byte
// 7 bytes PADDING (for b's 8-byte alignment)
b int64 // 8 bytes
c bool // 1 byte
// 7 bytes PADDING (to align next field / end of struct)
} // Total: 24 bytes (wastes 14 bytes)
// Well ordered — no padding (fields largest to smallest)
type Compact struct {
b int64 // 8 bytes
a bool // 1 byte
c bool // 1 byte
// 6 bytes padding to align to 8-byte boundary at struct end
} // Total: 16 bytes (saves 8 bytes vs Wasteful)
// Verify with unsafe
fmt.Println(unsafe.Sizeof(Wasteful{})) // 24
fmt.Println(unsafe.Sizeof(Compact{})) // 16
// Performance impact:
// Larger structs → more cache lines → more cache misses in hot loops
// A 50% size reduction can be a 2x performance improvement in array iteration
// Tool: fieldalignment (golang.org/x/tools/go/analysis/passes/fieldalignment)
// go install golang.org/x/tools/cmd/fieldalignment@latest
// fieldalignment ./... — reports suboptimal struct layouts
// fieldalignment -fix ./.. — rewrites structs (review before committing!)
// go vet includes a similar check with: -structtag flagThe rule of thumb: order struct fields from largest to smallest alignment requirement. In practice: int64/float64/uintptr (8 bytes) first, then int32/float32 (4 bytes), then int16 (2 bytes), then bool/int8/byte (1 byte) last. This is particularly important for structs that appear in large arrays or slices processed in hot paths.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
