Go: Under the Hood Under the Hood
中文

Chapter 5 Data Structures

Data dominates. If you've chosen the right data structures and organized things well, the algorithms will almost always be self-evident.
-- Rob Pike, "Notes on Programming in C"

Slices, strings and maps are the three data structures most used in Go programs. Their syntax is simple, but each rests on its own runtime layout and growth strategy. When append copies, when slices share memory, when a string conversion allocates, why a map iterates in random order and cannot be written concurrently: the answers to all of these follow from layout and implementation, and need not be memorized as isolated rules.

The chapter has four sections. 5.1 covers the memory layout of arrays and slices, their growth strategy, and what the compiler does to reduce allocations. 5.2 covers string immutability, UTF-8 and zero-copy conversion. 5.3 covers the general principles of hash tables and hash-flooding attacks. 5.4 covers the map implementation of Go 1.24, which is based on the Swiss Table.