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-structures
3description: "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)."
4user-invocable: true
5license: MIT
6compatibility: Designed for Claude Code, Codex or similar harness, and for projects using Golang.
7metadata:
8 author: samber
9 version: "1.2.2"
10 openclaw:
11 emoji: "🗃"
12 homepage: https://github.com/samber/cc-skills-golang
13 requires:
14 bins:
15 - go
16 install: []
17allowed-tools: Read Edit Write Glob Grep Bash(go:*) Bash(golangci-lint:*) Bash(git:*) Agent Bash(godig:*) Bash(gopls:*) LSP mcp__gopls__* mcp__context7__resolve-library-id mcp__context7__query-docs
18paths:
19 - "**/*.go"
20---
21
22**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.
23
24# Go Data Structures
25
26Built-in and standard library data structures: internals, correct usage, and selection guidance.
27
28- For safety pitfalls (nil maps, append aliasing, defensive copies) see `samber/cc-skills-golang@golang-safety` skill.
29- For channels and sync primitives see `samber/cc-skills-golang@golang-concurrency` skill.
30- For string/byte/rune choice see `samber/cc-skills-golang@golang-design-patterns` skill.
31
32## Best Practices Summary
33
341. **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
352. **Arrays** SHOULD be preferred over slices only for fixed, compile-time-known sizes (hash digests, IPv4 addresses, matrix dimensions)
363. **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
374. **Use `container/heap`** for priority queues, **`container/list`** only when frequent middle insertions are needed, **`container/ring`** for fixed-size circular buffers
385. **`strings.Builder`** MUST be preferred for building strings; **`bytes.Buffer`** MUST be preferred for bidirectional I/O (implements both `io.Reader` and `io.Writer`)
396. Generic data structures SHOULD use the **tightest constraint** possible — `comparable` for keys, custom interfaces for ordering
407. **`unsafe.Pointer`** MUST only follow the 6 valid conversion patterns from the Go spec — NEVER store in a `uintptr` variable across statements
418. **`weak.Pointer[T]`** (Go 1.24+) SHOULD be used for caches and canonicalization maps to allow GC to reclaim entries
42
43## Slice Internals
44
45A 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).
46
47### Capacity Growth
48
49- < 256 elements: capacity doubles
50- > = 256 elements: grows by ~25% (`newcap += (newcap + 3*256) / 4`)
51- Each growth copies the entire backing array — O(n)
52
53### Preallocation
54
55```go
56// Exact size known
57users := make([]User, 0, len(ids))
58
59// Approximate size known
60results := make([]Result, 0, estimatedCount)
61
62// Pre-grow before bulk append (Go 1.21+)
63s = slices.Grow(s, additionalNeeded)
64```
65
66### `slices` Package (Go 1.21+)
67
68Key functions: `Sort`/`SortFunc`, `BinarySearch`, `Contains`, `Compact`, `Grow`. For `Clone`, `Equal`, `DeleteFunc` → see `samber/cc-skills-golang@golang-safety` skill.
69
70**[Slice Internals Deep Dive](./references/slice-internals.md)** — Full `slices` package reference, growth mechanics, `len` vs `cap`, header copying, backing array aliasing.
71
72## Map Internals
73
74Maps are hash tables with 8-entry buckets and overflow chains. They are reference types — assigning a map copies the pointer, not the data.
75
76### Preallocation
77
78```go
79m := make(map[string]*User, len(users)) // avoids rehashing during population
80```
81
82### `maps` Package Quick Reference (Go 1.21+)
83
84| Function | Purpose |
85| ----------------- | ---------------------------- |
86| `Collect` (1.23+) | Build map from iterator |
87| `Insert` (1.23+) | Insert entries from iterator |
88| `All` (1.23+) | Iterator over all entries |
89| `Keys`, `Values` | Iterators over keys/values |
90
91For `Clone`, `Equal`, sorted iteration → see `samber/cc-skills-golang@golang-safety` skill.
92
93**[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.
94
95## Arrays
96
97Fixed-size, value types. Copied entirely on assignment. Use for compile-time-known sizes:
98
99```go
100type Digest [32]byte // fixed-size, value type
101var grid [3][3]int // multi-dimensional
102cache := map[[2]int]Result{} // arrays are comparable — usable as map keys
103```
104
105Prefer slices for everything else — arrays cannot grow and pass by value (expensive for large sizes).
106
107## container/ Standard Library
108
109| Package | Data Structure | Best For |
110| --- | --- | --- |
111| `container/list` | Doubly-linked list | LRU caches, frequent middle insertion/removal |
112| `container/heap` | Min-heap (priority queue) | Top-K, scheduling, Dijkstra |
113| `container/ring` | Circular buffer | Rolling windows, round-robin |
114| `bufio` | Buffered reader/writer/scanner | Efficient I/O with small reads/writes |
115
116Container 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.
117
118## strings.Builder vs bytes.Buffer
119
120Use `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)**
121
122## Generic Collections (Go 1.18+)
123
124Use the tightest constraint possible. `comparable` for map keys, `cmp.Ordered` for sorting, custom interfaces for domain-specific ordering.
125
126```go
127type Set[T comparable] map[T]struct{}
128
129func (s Set[T]) Add(v T) { s[v] = struct{}{} }
130func (s Set[T]) Contains(v T) bool { _, ok := s[v]; return ok }
131```
132
133**[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.
134
135## Pointer Types
136
137| Type | Use Case | Zero Value |
138| --- | --- | --- |
139| `*T` | Normal indirection, mutation, optional values | `nil` |
140| `unsafe.Pointer` | FFI, low-level memory layout (6 spec patterns only) | `nil` |
141| `weak.Pointer[T]` (1.24+) | Caches, canonicalization, weak references | N/A |
142
143**[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.
144
145## Copy Semantics Quick Reference
146
147| Type | Copy Behavior | Independence |
148| --- | --- | --- |
149| `int`, `float`, `bool`, `string` | Value (deep copy) | Fully independent |
150| `array`, `struct` | Value (deep copy) | Fully independent |
151| `slice` | Header copied, backing array shared | Use `slices.Clone` |
152| `map` | Reference copied | Use `maps.Clone` |
153| `channel` | Reference copied | Same channel |
154| `*T` (pointer) | Address copied | Same underlying value |
155| `interface` | Value copied (type + value pair) | Depends on held type |
156
157## Third-Party Libraries
158
159For advanced data structures (trees, sets, queues, stacks) beyond the standard library:
160
161- **`emirpasic/gods`** — comprehensive collection library (trees, sets, lists, stacks, maps, queues)
162- **`deckarep/golang-set`** — thread-safe and non-thread-safe set implementations
163- **`gammazero/deque`** — fast double-ended queue
164
165When using third-party libraries, refer to their official documentation and code examples for current API signatures.
166
167- 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.
168- To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`).
169- Context7 remains a fallback for docs not indexed on pkg.go.dev.
170
171## Cross-References
172
173- → See `samber/cc-skills-golang@golang-performance` skill for struct field alignment, memory layout optimization, and cache locality
174- → See `samber/cc-skills-golang@golang-safety` skill for nil map/slice pitfalls, append aliasing, defensive copying, `slices.Clone`/`Equal`
175- → See `samber/cc-skills-golang@golang-concurrency` skill for channels, `sync.Map`, `sync.Pool`, and all sync primitives
176- → See `samber/cc-skills-golang@golang-design-patterns` skill for `string` vs `[]byte` vs `[]rune`, iterators, streaming
177- → See `samber/cc-skills-golang@golang-structs-interfaces` skill for struct composition, embedding, and generics vs `any`
178- → See `samber/cc-skills-golang@golang-code-style` skill for slice/map initialization style
179
180## Common Mistakes
181
182| Mistake | Fix |
183| --- | --- |
184| 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` |
185| Using `container/list` when a slice would suffice | Linked lists have poor cache locality (each node is a separate heap allocation). Benchmark first |
186| `bytes.Buffer` for pure string building | Buffer's `String()` copies the underlying bytes. `strings.Builder` avoids this copy |
187| `unsafe.Pointer` stored as `uintptr` across statements | GC can move the object between statements — the `uintptr` becomes a dangling reference |
188| 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 |
189
190## References
191
192- [Go Data Structures (Russ Cox)](https://research.swtch.com/godata)
193- [The Go Memory Model](https://go.dev/ref/mem)
194- [Effective Go](https://go.dev/doc/effective_go)