Chapter 03Allocation and EscapePaid chapter

Memory Layout — Alignment, Padding, and False Sharing

Understand struct alignment and padding, how field ordering changes size and cache density, and how false sharing turns independent atomics into a contended hotspot.

Launch paywall

This chapter is part of the paid launch. The preview stays visible so readers can evaluate the exact scope before buying.

Included in

Core and Complete. Complete also includes the practical exam and free Go-version updates.

Preview

What this chapter teaches

Common belief

Struct size is the sum of its fields, and field order is cosmetic.

What actually happens

alignment rules insert padding, so field order changes unsafe.Sizeof with zero behavioral change; and two unrelated fields sharing a cache line can silently destroy parallel scaling (false sharing).

Mechanisms covered
Memory is fetched in cache lines (64 bytes on amd64/arm64). Two variables in the same line are a unit to the cache-coherence protocol: a write to one invalidates the other's line on other cores — "false sharing.
unsafe.Sizeof/Alignof/Offsetof expose the layout exactly; go vet -fieldalignment (and betteralign) flag wasteful ordering.
experimentbench
One-time purchase

Unlock Chapter 03 and the rest of the course.

Core unlocks every paid chapter and lab. Complete also includes the practical exam and future Go-version updates.

Core
$49

Full course access with the complete mechanics curriculum.

All paid chapters unlocked
One-time purchase
Perfect if you want the material only
Complete
$79
Best value

Best-value launch option with the exam and free Go-version course updates.

Everything in Core
Black Belt practical exam
Free Go-version updates

No subscription or account required. Access is tied to the email entered at Stripe Checkout and can be restored by email.

Free chapter
Escape Analysis — The Compiler's Fragile Decision
Understand how the compiler decides between stack and heap allocation, why the decision is fragile, and how to verify it.
Free chapter
Maps — Swiss Tables, Growth, and Permanent Memory
Understand the Go 1.24+ Swiss Table map layout, why maps don't shrink, and how to design for memory reclamation.