cxuu avatar

Go Data Structures

Handles the practical decisions you run into when working with Go's core collections: when to use make versus a nil slice, why append needs assignment, how to implement sets with maps, and the aliasing traps with copying structs that have pointer receivers. The decision tree at the top is genuinely useful for picking between arrays, slices, and maps. Draws from Effective Go and the major style guides, so the advice is standard practice rather than opinion. Worth having open when you're writing collection manipulation code and want to avoid the common gotchas, especially around JSON marshaling of empty slices and the mutex copying footgun.

skills.sh installs
1.3K
GitHub stars
193
last updated
Aug 21, 2026

Install

$ npx -y skills add cxuu/golang-skills --skill go-data-structures --agent claude-code

Installs into .claude/skills of the current project.

Files

Go Data Structures

Resource Routing

  • references/SLICES.md - Read when deciding nil versus empty slices, copying slices, or managing slice capacity and aliasing.

Choosing a Data Structure

What do you need?
├─ Ordered collection of items
│  ├─ Fixed size known at compile time → Array [N]T
│  └─ Dynamic size → Slice []T
│     ├─ Know approximate size? → make([]T, 0, capacity)
│     └─ Unknown size or nil-safe for JSON? → var s []T (nil)
├─ Key-value lookup
│  └─ Map map[K]V
│     ├─ Know approximate size? → make(map[K]V, capacity)
│     └─ Need a set? → map[T]struct{} (zero-size values)
└─ Need to pass to a function?
   └─ Copy at the boundary if the caller might mutate it

When this skill does NOT apply: For concurrent access to data structures (mutexes, atomic operations), see go-concurrency. For defensive copying at API boundaries, see go-defensive. For pre-sizing capacity for performance, see go-performance.


Slices

The append Function

Always assign the result — the underlying array may change:

x := []int{1, 2, 3}
x = append(x, 4, 5, 6)

Sponsors

Details

Repository
github.com/cxuu/golang-skills
Category
Go
First seen
May 16, 2026

More from cxuu/golang-skills

All 20

Recommended

More Go
1cxuu avatargo-functional-optionscxuu/golang-skillsUse when designing a Go constructor or factory function with optional configuration — especially with 3+ optional parameters or extensible APIs. Also use when b1.3Kskills.sh installs
2cxuu avatargo-control-flowcxuu/golang-skillsUse when writing conditionals, loops, or switch statements in Go — including if with initialization, early returns, for loop forms, range, switch, type switches1.3Kskills.sh installs
3cxuu avatargo-loggingcxuu/golang-skillsUse when choosing a logging approach, configuring slog, writing structured log statements, or deciding log levels in Go. Also use when setting up production log1.1Kskills.sh installs
4cxuu avatargo-functionscxuu/golang-skillsUse when organizing functions within a Go file, formatting function signatures, designing return values, or following Printf-style naming conventions. Also use 1.1Kskills.sh installs
5cxuu avatargo-genericscxuu/golang-skillsUse when deciding whether to use Go generics, writing generic functions or types, choosing constraints, or picking between type aliases and type definitions. Al1.1Kskills.sh installs
6cxuu avatargo-declarationscxuu/golang-skillsUse when declaring or initializing Go variables, constants, structs, or maps — including var vs :=, reducing scope with if-init, formatting composite literals, 1.1Kskills.sh installs