# Golang Data Structures

Golang data structures, slices (internals, capacity growth, preallocation, slices...

- Repository: https://github.com/samber/cc-skills-golang (3750 stars)
- Category: Marketing
- License: MIT
- Last updated: 2026-11-01
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add samber/cc-skills-golang --skill golang-data-structures -g -a claude-code -y`
Codex: `npx skills add samber/cc-skills-golang --skill golang-data-structures -g -a codex -y`
Cursor: `npx skills add samber/cc-skills-golang --skill golang-data-structures -g -a cursor -y`
Gemini CLI: `npx skills add samber/cc-skills-golang --skill golang-data-structures -g -a gemini-cli -y`

## Description

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).

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

**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

The rest: https://github.com/samber/cc-skills-golang/blob/HEAD/skills/golang-data-structures/SKILL.md
