Persona: You are a Go engineer who understands data structure internals. You choose the right structure for the job — not the most familiar one — by reasoning about memory layout, allocation cost, and access patterns.
Go Data Structures
Built-in and standard library data structures: internals, correct usage, and selection guidance.
- For safety pitfalls (nil maps, append aliasing, defensive copies) see
samber/cc-skills-golang@golang-safety skill.
- For channels and sync primitives see
samber/cc-skills-golang@golang-concurrency skill.
- For string/byte/rune choice see
samber/cc-skills-golang@golang-design-patterns skill.
Best Practices Summary
- Preallocate slices and maps with
make(T, 0, n) / make(map[K]V, n) when size is known or estimable — avoids repeated growth copies and rehashing
- Arrays SHOULD be preferred over slices only for fixed, compile-time-known sizes (hash digests, IPv4 addresses, matrix dimensions)
- NEVER rely on slice capacity growth timing — the growth algorithm changed between Go versions and may change again; your code should not depend on when a new backing array is allocated
- Use
container/heap for priority queues, container/list only when frequent middle insertions are needed, container/ring for fixed-size circular buffers
strings.Builder MUST be preferred for building strings; bytes.Buffer MUST be preferred for bidirectional I/O (implements both io.Reader and io.Writer)
- Generic data structures SHOULD use the tightest constraint possible —
comparable for keys, custom interfaces for ordering
unsafe.Pointer MUST only follow the 6 valid conversion patterns from the Go spec — NEVER store in a uintptr variable across statements
weak.Pointer[T] (Go 1.24+) SHOULD be used for caches and canonicalization maps to allow GC to reclaim entries
Slice Internals
A slice is a 3-word header: pointer, length, capacity. Multiple slices can share a backing array (→ see samber/cc-skills-golang@golang-safety for aliasing traps and the header diagram).
Capacity Growth
- < 256 elements: capacity doubles
= 256 elements: grows by ~25% (newcap += (newcap + 3*256) / 4)
- Each growth copies the entire backing array — O(n)
Preallocation
// Exact size known
users := make([]User, 0, len(ids))
// Approximate size known
results := make([]Result, 0, estimatedCount)
// Pre-grow before bulk append (Go 1.21+)
s = slices.Grow(s, additionalNeeded)
slices Package (Go 1.21+)
Key functions: Sort/SortFunc, BinarySearch, Contains, Compact, Grow. For Clone, Equal, DeleteFunc → see samber/cc-skills-golang@golang-safety skill.
Slice Internals Deep Dive — Full slices package reference, growth mechanics, len vs cap, header copying, backing array aliasing.
Map Internals
Maps are hash tables with 8-entry buckets and overflow chains. They are reference types — assigning a map copies the pointer, not the data.
Preallocation
m := make(map[string]*User, len(users)) // avoids rehashing during population
maps Package Quick Reference (Go 1.21+)
| Function |
Purpose |
Collect (1.23+) |
Build map from iterator |
Insert (1.23+) |
Insert entries from iterator |
All (1.23+) |
Iterator over all entries |
Keys, Values |
Iterators over keys/values |
For Clone, Equal, sorted iteration → see samber/cc-skills-golang@golang-safety skill.
Map Internals Deep Dive — How Go maps store and hash data, bucket overflow chains, why maps never shrink (and what to do about it), comparing map performance to alternatives.
Arrays
Fixed-size, value types. Copied entirely on assignment. Use for compile-time-known sizes:
type Digest [32]byte // fixed-size, value type
var grid [3][3]int // multi-dimensional
cache := map[[2]int]Result{} // arrays are comparable — usable as map keys
Prefer slices for everything else — arrays cannot grow and pass by value (expensive for large sizes).
container/ Standard Library
| Package |
Data Structure |
Best For |
container/list |
Doubly-linked list |
LRU caches, frequent middle insertion/removal |
container/heap |
Min-heap (priority queue) |
Top-K, scheduling, Dijkstra |
container/ring |
Circular buffer |
Rolling windows, round-robin |
bufio |
Buffered reader/writer/scanner |
Efficient I/O with small reads/writes |
Container types use any (no type safety) — consider generic wrappers. Container Patterns, bufio, and Examples — When to use each container type, generic wrappers to add type safety, and bufio patterns for efficient I/O.
strings.Builder vs bytes.Buffer
Use strings.Builder for pure string concatenation (avoids copy on String()), bytes.Buffer when you need io.Reader or byte manipulation. Both support Grow(n). Details and comparison
Generic Collections (Go 1.18+)
Use the tightest constraint possible. comparable for map keys, cmp.Ordered for sorting, custom interfaces for domain-specific ordering.
type Set[T comparable] map[T]struct{}
func (s Set[T]) Add(v T) { s[v] = struct{}{} }
func (s Set[T]) Contains(v T) bool { _, ok := s[v]; return ok }
Writing Generic Data Structures — Using Go 1.18+ generics for type-safe containers, understanding constraint satisfaction, and building domain-specific generic types.
Pointer Types
| Type |
Use Case |
Zero Value |
*T |
Normal indirection, mutation, optional values |
nil |
unsafe.Pointer |
FFI, low-level memory layout (6 spec patterns only) |
nil |
weak.Pointer[T] (1.24+) |
Caches, canonicalization, weak references |
N/A |
Pointer Types Deep Dive — Normal pointers, unsafe.Pointer (the 6 valid spec patterns), and weak.Pointer[T] for GC-safe caches that don't prevent cleanup.
Copy Semantics Quick Reference
| Type |
Copy Behavior |
Independence |
int, float, bool, string |
Value (deep copy) |
Fully independent |
array, struct |
Value (deep copy) |
Fully independent |
slice |
Header copied, backing array shared |
Use slices.Clone |
map |
Reference copied |
Use maps.Clone |
channel |
Reference copied |
Same channel |
*T (pointer) |
Address copied |
Same underlying value |
interface |
Value copied (type + value pair) |
Depends on held type |
Third-Party Libraries
For advanced data structures (trees, sets, queues, stacks) beyond the standard library:
emirpasic/gods — comprehensive collection library (trees, sets, lists, stacks, maps, queues)
deckarep/golang-set — thread-safe and non-thread-safe set implementations
gammazero/deque — fast double-ended queue
When using third-party libraries, refer to their official documentation and code examples for current API signatures.
- For Go package docs, symbols, versions, importers, and known vulnerabilities, → See
samber/cc-skills-golang@golang-pkg-go-dev skill (godig) — prefer it over Context7 for Go package facts.
- To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See
samber/cc-skills-golang@golang-gopls skill (gopls).
- Context7 remains a fallback for docs not indexed on pkg.go.dev.
Cross-References
- → See
samber/cc-skills-golang@golang-performance skill for struct field alignment, memory layout optimization, and cache locality
- → See
samber/cc-skills-golang@golang-safety skill for nil map/slice pitfalls, append aliasing, defensive copying, slices.Clone/Equal
- → See
samber/cc-skills-golang@golang-concurrency skill for channels, sync.Map, sync.Pool, and all sync primitives
- → See
samber/cc-skills-golang@golang-design-patterns skill for string vs []byte vs []rune, iterators, streaming
- → See
samber/cc-skills-golang@golang-structs-interfaces skill for struct composition, embedding, and generics vs any
- → See
samber/cc-skills-golang@golang-code-style skill for slice/map initialization style
Common Mistakes
| Mistake |
Fix |
| Growing a slice in a loop without preallocation |
Each growth copies the entire backing array — O(n) per growth. Use make([]T, 0, n) or slices.Grow |
Using container/list when a slice would suffice |
Linked lists have poor cache locality (each node is a separate heap allocation). Benchmark first |
bytes.Buffer for pure string building |
Buffer's String() copies the underlying bytes. strings.Builder avoids this copy |
unsafe.Pointer stored as uintptr across statements |
GC can move the object between statements — the uintptr becomes a dangling reference |
| Large struct values in maps (copying overhead) |
Map access copies the entire value. Use map[K]*V for large value types to avoid the copy |
References
1---2name: golang-data-structures3description: Golang data structures — slices (internals, capacity growth, preallocation, slices package), maps (internals, hash buckets, maps package), arrays, container/list/heap/ring, strings.Builder vs bytes.Buffer, generic collections, pointers (unsafe.Pointer, weak.Pointer), and copy semantics. Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals. Not for applying optimization patterns once profiling has identified a bottleneck (→ See `samber/cc-skills-golang@golang-performance` skill).4license: MIT5---6
7**Persona:** You are a Go engineer who understands data structure internals. You choose the right structure for the job — not the most familiar one — by reasoning about memory layout, allocation cost, and access patterns.
8
9# Go Data Structures
10
11Built-in and standard library data structures: internals, correct usage, and selection guidance.
12
13- For safety pitfalls (nil maps, append aliasing, defensive copies) see `samber/cc-skills-golang@golang-safety` skill.
14- For channels and sync primitives see `samber/cc-skills-golang@golang-concurrency` skill.
15- For string/byte/rune choice see `samber/cc-skills-golang@golang-design-patterns` skill.
16
17## Best Practices Summary
18
191. **Preallocate slices and maps** with `make(T, 0, n)` / `make(map[K]V, n)` when size is known or estimable — avoids repeated growth copies and rehashing
202. **Arrays** SHOULD be preferred over slices only for fixed, compile-time-known sizes (hash digests, IPv4 addresses, matrix dimensions)
213. **NEVER rely on slice capacity growth timing** — the growth algorithm changed between Go versions and may change again; your code should not depend on when a new backing array is allocated
224. **Use `container/heap`** for priority queues, **`container/list`** only when frequent middle insertions are needed, **`container/ring`** for fixed-size circular buffers
235. **`strings.Builder`** MUST be preferred for building strings; **`bytes.Buffer`** MUST be preferred for bidirectional I/O (implements both `io.Reader` and `io.Writer`)
246. Generic data structures SHOULD use the **tightest constraint** possible — `comparable` for keys, custom interfaces for ordering
257. **`unsafe.Pointer`** MUST only follow the 6 valid conversion patterns from the Go spec — NEVER store in a `uintptr` variable across statements
268. **`weak.Pointer[T]`** (Go 1.24+) SHOULD be used for caches and canonicalization maps to allow GC to reclaim entries
27
28## Slice Internals
29
30A slice is a 3-word header: pointer, length, capacity. Multiple slices can share a backing array (→ see `samber/cc-skills-golang@golang-safety` for aliasing traps and the header diagram).
31
32### Capacity Growth
33
34- < 256 elements: capacity doubles
35- > = 256 elements: grows by ~25% (`newcap += (newcap + 3*256) / 4`)
36- Each growth copies the entire backing array — O(n)
37
38### Preallocation
39
40```go
41// Exact size known
42users := make([]User, 0, len(ids))
43
44// Approximate size known
45results := make([]Result, 0, estimatedCount)
46
47// Pre-grow before bulk append (Go 1.21+)
48s = slices.Grow(s, additionalNeeded)
49```
50
51### `slices` Package (Go 1.21+)
52
53Key functions: `Sort`/`SortFunc`, `BinarySearch`, `Contains`, `Compact`, `Grow`. For `Clone`, `Equal`, `DeleteFunc` → see `samber/cc-skills-golang@golang-safety` skill.
54
55**[Slice Internals Deep Dive](./references/slice-internals.md)** — Full `slices` package reference, growth mechanics, `len` vs `cap`, header copying, backing array aliasing.
56
57## Map Internals
58
59Maps are hash tables with 8-entry buckets and overflow chains. They are reference types — assigning a map copies the pointer, not the data.
60
61### Preallocation
62
63```go
64m := make(map[string]*User, len(users)) // avoids rehashing during population
65```
66
67### `maps` Package Quick Reference (Go 1.21+)
68
69| Function | Purpose |
70| ----------------- | ---------------------------- |
71| `Collect` (1.23+) | Build map from iterator |
72| `Insert` (1.23+) | Insert entries from iterator |
73| `All` (1.23+) | Iterator over all entries |
74| `Keys`, `Values` | Iterators over keys/values |
75
76For `Clone`, `Equal`, sorted iteration → see `samber/cc-skills-golang@golang-safety` skill.
77
78**[Map Internals Deep Dive](./references/map-internals.md)** — How Go maps store and hash data, bucket overflow chains, why maps never shrink (and what to do about it), comparing map performance to alternatives.
79
80## Arrays
81
82Fixed-size, value types. Copied entirely on assignment. Use for compile-time-known sizes:
83
84```go
85type Digest [32]byte // fixed-size, value type
86var grid [3][3]int // multi-dimensional
87cache := map[[2]int]Result{} // arrays are comparable — usable as map keys
88```
89
90Prefer slices for everything else — arrays cannot grow and pass by value (expensive for large sizes).
91
92## container/ Standard Library
93
94| Package | Data Structure | Best For |
95| --- | --- | --- |
96| `container/list` | Doubly-linked list | LRU caches, frequent middle insertion/removal |
97| `container/heap` | Min-heap (priority queue) | Top-K, scheduling, Dijkstra |
98| `container/ring` | Circular buffer | Rolling windows, round-robin |
99| `bufio` | Buffered reader/writer/scanner | Efficient I/O with small reads/writes |
100
101Container types use `any` (no type safety) — consider generic wrappers. **[Container Patterns, bufio, and Examples](./references/containers.md)** — When to use each container type, generic wrappers to add type safety, and `bufio` patterns for efficient I/O.
102
103## strings.Builder vs bytes.Buffer
104
105Use `strings.Builder` for pure string concatenation (avoids copy on `String()`), `bytes.Buffer` when you need `io.Reader` or byte manipulation. Both support `Grow(n)`. **[Details and comparison](./references/containers.md)**
106
107## Generic Collections (Go 1.18+)
108
109Use the tightest constraint possible. `comparable` for map keys, `cmp.Ordered` for sorting, custom interfaces for domain-specific ordering.
110
111```go
112type Set[T comparable] map[T]struct{}
113
114func (s Set[T]) Add(v T) { s[v] = struct{}{} }
115func (s Set[T]) Contains(v T) bool { _, ok := s[v]; return ok }
116```
117
118**[Writing Generic Data Structures](./references/generics.md)** — Using Go 1.18+ generics for type-safe containers, understanding constraint satisfaction, and building domain-specific generic types.
119
120## Pointer Types
121
122| Type | Use Case | Zero Value |
123| --- | --- | --- |
124| `*T` | Normal indirection, mutation, optional values | `nil` |
125| `unsafe.Pointer` | FFI, low-level memory layout (6 spec patterns only) | `nil` |
126| `weak.Pointer[T]` (1.24+) | Caches, canonicalization, weak references | N/A |
127
128**[Pointer Types Deep Dive](./references/pointers.md)** — Normal pointers, `unsafe.Pointer` (the 6 valid spec patterns), and `weak.Pointer[T]` for GC-safe caches that don't prevent cleanup.
129
130## Copy Semantics Quick Reference
131
132| Type | Copy Behavior | Independence |
133| --- | --- | --- |
134| `int`, `float`, `bool`, `string` | Value (deep copy) | Fully independent |
135| `array`, `struct` | Value (deep copy) | Fully independent |
136| `slice` | Header copied, backing array shared | Use `slices.Clone` |
137| `map` | Reference copied | Use `maps.Clone` |
138| `channel` | Reference copied | Same channel |
139| `*T` (pointer) | Address copied | Same underlying value |
140| `interface` | Value copied (type + value pair) | Depends on held type |
141
142## Third-Party Libraries
143
144For advanced data structures (trees, sets, queues, stacks) beyond the standard library:
145
146- **`emirpasic/gods`** — comprehensive collection library (trees, sets, lists, stacks, maps, queues)
147- **`deckarep/golang-set`** — thread-safe and non-thread-safe set implementations
148- **`gammazero/deque`** — fast double-ended queue
149
150When using third-party libraries, refer to their official documentation and code examples for current API signatures.
151
152- For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts.
153- To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`).
154- Context7 remains a fallback for docs not indexed on pkg.go.dev.
155
156## Cross-References
157
158- → See `samber/cc-skills-golang@golang-performance` skill for struct field alignment, memory layout optimization, and cache locality
159- → See `samber/cc-skills-golang@golang-safety` skill for nil map/slice pitfalls, append aliasing, defensive copying, `slices.Clone`/`Equal`
160- → See `samber/cc-skills-golang@golang-concurrency` skill for channels, `sync.Map`, `sync.Pool`, and all sync primitives
161- → See `samber/cc-skills-golang@golang-design-patterns` skill for `string` vs `[]byte` vs `[]rune`, iterators, streaming
162- → See `samber/cc-skills-golang@golang-structs-interfaces` skill for struct composition, embedding, and generics vs `any`
163- → See `samber/cc-skills-golang@golang-code-style` skill for slice/map initialization style
164
165## Common Mistakes
166
167| Mistake | Fix |
168| --- | --- |
169| Growing a slice in a loop without preallocation | Each growth copies the entire backing array — O(n) per growth. Use `make([]T, 0, n)` or `slices.Grow` |
170| Using `container/list` when a slice would suffice | Linked lists have poor cache locality (each node is a separate heap allocation). Benchmark first |
171| `bytes.Buffer` for pure string building | Buffer's `String()` copies the underlying bytes. `strings.Builder` avoids this copy |
172| `unsafe.Pointer` stored as `uintptr` across statements | GC can move the object between statements — the `uintptr` becomes a dangling reference |
173| Large struct values in maps (copying overhead) | Map access copies the entire value. Use `map[K]*V` for large value types to avoid the copy |
174
175## References
176
177- [Go Data Structures (Russ Cox)](https://research.swtch.com/godata)
178- [The Go Memory Model](https://go.dev/ref/mem)
179- [Effective Go](https://go.dev/doc/effective_go)