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, Complete, and Team. 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
Email-first checkoutUnlock Chapter 03

Unlock Chapter 03 and the rest of the course.

Start with your email, then continue to Lemon Squeezy checkout. Core unlocks the paid chapters, Complete adds the exam and free Go-version updates, and Team gives you 5 seats.

Choose a plan

You'll head to Lemon Squeezy for secure checkout. Global tax is handled there. No subscription. Complete also includes the practical exam and free Go-version updates.

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.