golang-data-structures

作者: samber

Golang 数据结构 — 切片(内部机制、容量增长、预分配、slices 包)、映射(内部机制、哈希桶、maps 包)、数组、container/list/heap/ring、strings.Builder 与 bytes.Buffer、泛型集合、指针(unsafe.Pointer、weak.Pointer)以及复制语义。适用于选择或优化 Go 数据结构、实现泛型容器、使用 container/ 包、unsafe 或 weak 指针,或对切片/映射内部机制有疑问时。

npx skills add https://github.com/samber/cc-skills-golang --skill golang-data-structures

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

  1. 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
  2. Arrays SHOULD be preferred over slices only for fixed, compile-time-known sizes (hash digests, IPv4 addresses, matrix dimensions)
  3. 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
  4. Use container/heap for priority queues, container/list only when frequent middle insertions are needed, container/ring for fixed-size circular buffers
  5. strings.Builder MUST be preferred for building strings; bytes.Buffer MUST be preferred for bidirectional I/O (implements both io.Reader and io.Writer)
  6. Generic data structures SHOULD use the tightest constraint possible — comparable for keys, custom interfaces for ordering
  7. unsafe.Pointer MUST only follow the 6 valid conversion patterns from the Go spec — NEVER store in a uintptr variable across statements
  8. 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+)

FunctionPurpose
Collect (1.23+)Build map from iterator
Insert (1.23+)Insert entries from iterator
All (1.23+)Iterator over all entries
Keys, ValuesIterators 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

PackageData StructureBest For
container/listDoubly-linked listLRU caches, frequent middle insertion/removal
container/heapMin-heap (priority queue)Top-K, scheduling, Dijkstra
container/ringCircular bufferRolling windows, round-robin
bufioBuffered reader/writer/scannerEfficient 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

TypeUse CaseZero Value
*TNormal indirection, mutation, optional valuesnil
unsafe.PointerFFI, low-level memory layout (6 spec patterns only)nil
weak.Pointer[T] (1.24+)Caches, canonicalization, weak referencesN/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

TypeCopy BehaviorIndependence
int, float, bool, stringValue (deep copy)Fully independent
array, structValue (deep copy)Fully independent
sliceHeader copied, backing array sharedUse slices.Clone
mapReference copiedUse maps.Clone
channelReference copiedSame channel
*T (pointer)Address copiedSame underlying value
interfaceValue 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. Context7 can help as a discoverability platform.

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

MistakeFix
Growing a slice in a loop without preallocationEach 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 sufficeLinked lists have poor cache locality (each node is a separate heap allocation). Benchmark first
bytes.Buffer for pure string buildingBuffer's String() copies the underlying bytes. strings.Builder avoids this copy
unsafe.Pointer stored as uintptr across statementsGC 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

来自 samber 的更多技能

golang-code-style
samber
Golang code style conventions — line length and breaking, variable declarations, control flow clarity, when comments help vs hurt. Use when writing or reviewing Go code, asking about style or clarity, or establishing project coding standards. Not for naming conventions (→ See `samber/cc-skills-golang@golang-naming` skill), linter configuration (→ See `samber/cc-skills-golang@golang-lint` skill), or doc comments (→ See `samber/cc-skills-golang@golang-documentation` skill).
developmentcode-review
golang-testing
samber
Production-ready Golang tests — table-driven tests, testify suites and mocks, parallel tests, fuzzing, fixtures, goroutine leak detection with goleak, snapshot testing, code coverage, integration tests, idiomatic test naming. Use when writing or reviewing Go tests, choosing a testing approach, setting up Go test CI, or debugging flaky/slow tests. For testify-specific APIs see `samber/cc-skills-golang@golang-stretchr-testify`; for measurement methodology see...
developmenttestingcode-review
golang-design-patterns
samber
惯用的Go设计模式——函数选项、构造函数、错误流与级联、资源管理与生命周期、优雅关闭、弹性、架构、依赖注入、数据处理、流式处理等。适用于在架构模式间明确选择、实现函数选项、设计构造函数API、设置优雅关闭、应用弹性模式,或询问哪种惯用Go模式适合特定问题时。
developmentdesigncode-review
golang-error-handling
samber
Idiomatic Golang error handling — creation, wrapping with %w, errors.Is/As, errors.Join, custom error types, sentinel errors, panic/recover, the single handling rule, structured logging with slog, HTTP request logging middleware, and samber/oops for production errors. Built to make logs usable at scale with log aggregation 3rd-party tools. Apply when creating, wrapping, inspecting, or logging errors in Go code. For samber/oops specifics → See `samber/cc-skills-golang@golang-samber-oops`...
developmentcode-review
golang-performance
samber
Golang性能优化模式与方法论——若存在X瓶颈,则应用Y方案。涵盖分配减少、CPU效率、内存布局、GC调优、池化、缓存及热路径优化。适用于性能分析或基准测试已识别瓶颈时,需采用正确优化模式进行修复。亦适用于执行性能代码审查时,提出改进建议或可帮助快速识别性能增益的基准测试。不适用于测量方法论(→...
developmentcode-review
golang-security
samber
Golang的安全最佳实践与漏洞防护,涵盖注入(SQL、命令、XSS)、密码学、文件系统安全、网络安全、Cookie、密钥管理、内存安全及日志记录。适用于编写、审查或审计Go代码的安全性,或处理涉及加密、I/O、密钥管理、用户输入处理或身份验证的高风险代码。包含安全工具的配置。
securitycode-reviewdevelopment
golang-database
samber
Go数据库访问全面指南——参数化查询、结构体扫描、可空列、事务、隔离级别、SELECT FOR UPDATE、连接池、批处理、上下文传播及迁移工具。适用于编写、审查或调试与PostgreSQL、MariaDB、MySQL或SQLite交互的Golang代码;数据库测试;或关于database/sql、sqlx或pgx的问题。不生成数据库模式或迁移SQL。
developmentdatabase
golang-lint
samber
Golang项目的lint最佳实践与golangci-lint配置——运行linter、配置.golangci.yml、使用nolint指令抑制警告、解读lint输出以及选择linter。适用于配置golangci-lint、询问lint警告或nolint抑制、设置代码质量工具或选择linter时使用。当用户提及golangci-lint、go vet、staticcheck或revive时也可使用。
developmentcode-reviewtesting