
Golang Data Structures
Pick and optimize Go slices, maps, strings builders, container/ packages, and pointers with correct internals-aware guidance during implementation.
Overview
golang-data-structures is an agent skill for the Build phase that guides slice, map, string, container, and pointer choices in Go using internals-aware performance reasoning.
Install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-data-structuresWhat is this skill?
- Slices and maps: capacity growth, hash buckets, preallocation, and maps/slices package usage
- Standard containers: container/list, heap, ring; strings.Builder vs bytes.Buffer selection
- Generics for collections; pointers including unsafe.Pointer and weak.Pointer
- Cross-links to golang-safety and golang-concurrency for nil maps, append aliasing, and channels
Adoption & trust: 3.8k installs on skills.sh; 2k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
You are writing Go backend code but defaulting to familiar slices and maps without understanding allocation, aliasing, or stdlib alternatives.
Who is it for?
Backend Go developers optimizing handlers, caches, and in-memory indexes during active feature work.
Skip if: Greenfield language choice or DevOps deploy topics—use Go ecosystem skills for tooling, CI, and cloud separately.
When should I use this skill?
Use when choosing or optimizing Go data structures, implementing generic containers, using container/ packages, unsafe or weak pointers, or questioning slice/map internals.
What do I get? / Deliverables
You implement structures matched to access patterns with clearer memory behavior and pointers to safety and concurrency skills when edge cases appear.
- Structure selection and optimization guidance aligned to your access patterns
- Pointers to related safety and concurrency practices when relevant
Recommended Skills
Journey fit
Backend implementation is where data-structure choices directly affect memory, GC pressure, and API latency for Go services. Solo backend builders need slice/map internals, preallocation, and generic containers while writing handlers and storage layers—not after launch.
How it compares
Deep Go stdlib structure reference for agents, not a generic DSA interview cheat sheet.
Common Questions / FAQ
Who is golang-data-structures for?
Solo and indie builders writing Go services who want agent help choosing slices, maps, builders, and container packages with performance in mind.
When should I use golang-data-structures?
Use it in Build while designing APIs, rewriting hot loops, adding generic collections, or deciding between strings.Builder and bytes.Buffer.
Is golang-data-structures safe to install?
It may run go, golangci-lint, and git via allowed tools; verify the repo source and Security Audits panel on this Prism page before enabling Bash in untrusted projects.
Workflow Chain
Then invoke: golang safety, golang concurrency
SKILL.md
READMESKILL.md - 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 ```go // 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](./references/slice-internals.md)** — 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 ```go