Generated banner for Golang Data Structures
Generated logo for Golang Data Structures

Golang Data Structures

Samuel BertheCommunityCreated by samberDistributed by Samuel Berthe

  • Skill
  • Guidelines
  • Software development
  • MIT
  • No issues found

From the author

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).
— the description by samber

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides deep technical insights into Go's built-in and standard library data structures. It covers memory layout, allocation behavior, and performance characteristics for slices, maps, arrays, and container packages. It helps developers make informed decisions about when to use specific structures, how to preallocate memory to avoid overhead, and how to leverage Go's generic capabilities for type-safe collections.

What it does

  • Explains internal mechanics of slices, maps, and arrays.
  • Provides best practices for preallocation to optimize memory usage.
  • Guides the selection of standard library containers like heap, list, and ring.
  • Compares strings.Builder and bytes.Buffer for efficient string manipulation.
  • Details the use of generic constraints for type-safe data structures.
  • Outlines proper usage of pointer types, including unsafe and weak pointers.

When to use it

  • When choosing the most efficient data structure for a specific task.
  • When optimizing memory allocation for slices or maps.
  • When implementing custom generic containers.
  • When deciding between strings.Builder and bytes.Buffer.
  • When working with low-level memory via unsafe pointers.
  • When needing to understand the performance implications of Go's collection types.

Advantages

  • Reduces unnecessary memory allocations through preallocation guidance.
  • Improves code performance by selecting optimal data structures.
  • Ensures type safety when building generic collections.
  • Provides clear distinctions between similar tools like strings.Builder and bytes.Buffer.
  • Helps avoid common pitfalls related to slice growth and map internals.

Usage examples

“How should I initialize a slice when I know the approximate number of elements?”
The agent will recommend using make([]T, 0, estimatedCount) to preallocate the backing array, preventing multiple reallocations and copies during growth.
“When is it better to use container/list instead of a slice?”
The agent will explain that container/list is best for frequent middle insertions or removals, while noting that slices are generally preferred for cache locality.
“What is the difference between strings.Builder and bytes.Buffer?”
The agent will clarify that strings.Builder is optimized for string concatenation without extra copies, while bytes.Buffer is better for bidirectional I/O.
“How do I define a generic set in Go?”
The agent will provide a code example using map[T]struct{} with the comparable constraint, demonstrating how to implement Add, Remove, and Contains methods.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/samber/golang-data-structures/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip golang-data-structures.zip -d .agents/skills

Documentation by samber

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.

  • 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

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

Files

references/pointers.md

Pointer Types Deep Dive

Table of Contents

  • Regular Pointers (*T)

    • Stack vs Heap (Escape Analysis)

    • new(T) vs &T{}

  • unsafe.Pointer

    • The 6 Valid Patterns (from the Go spec)

    • Critical Rule: NEVER Store uintptr Across Statements

    • Modern Alternatives (prefer these)

  • weak.Pointer[T] (Go 1.24+)

    • Use Cases

    • runtime.AddCleanup vs runtime.SetFinalizer

Regular Pointers (*T)

Stack vs Heap (Escape Analysis)

Go's compiler decides whether to allocate on the stack or heap. A variable "escapes" to the heap when its lifetime extends beyond the function:

func noEscape() int {
    x := 42
    return x // x stays on stack — copied on return
}

func escapes() *int {
    x := 42
    return &x // x escapes to heap — pointer outlives function
}

Use go build -gcflags="-m" to see escape analysis decisions. Heap allocations add GC pressure — avoid unnecessary escapes in hot paths.

new(T) vs &T{}

Both allocate and return a pointer. &T{} is preferred because it allows field initialization:

p := new(Point)       // *Point with zero values
p := &Point{X: 1}     // *Point with initialized fields — preferred

unsafe.Pointer

unsafe.Pointer bypasses Go's type system for FFI and low-level memory manipulation. Only the 6 patterns from the Go spec are safe; any other pattern is undefined behavior.

The 6 Valid Patterns (from the Go spec)

These are the ONLY safe ways to use unsafe.Pointer. Any other pattern is undefined behavior.

Pattern 1: Convert *T to *U via unsafe.Pointer

// Reinterpret a float64 as its raw bits
f := 1.5
bits := *(*uint64)(unsafe.Pointer(&f))

Pattern 2: Convert unsafe.Pointer to uintptr and back (same expression)

// Pointer arithmetic — MUST be a single expression
p := unsafe.Pointer(uintptr(unsafe.Pointer(&s.field)) + offset)

Pattern 3: reflect.Value.Pointer() or UnsafeAddr() to unsafe.Pointer

p := unsafe.Pointer(reflect.ValueOf(&x).Pointer())

Pattern 4: syscall.Syscall arguments

syscall.Syscall(SYS_READ, fd, uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
Critical Rule: NEVER Store uintptr Across Statements
// ✗ DANGEROUS — GC can move the object between these two lines
u := uintptr(unsafe.Pointer(&x))
// ... GC may run here, moving x ...
p := unsafe.Pointer(u) // dangling pointer

// ✓ Safe — single expression
p := unsafe.Pointer(uintptr(unsafe.Pointer(&x)) + offset)
Modern Alternatives (prefer these)
FunctionSincePurpose
unsafe.Add(ptr, len)Go 1.17Pointer arithmetic without uintptr conversion
unsafe.Slice(ptr, len)Go 1.17Create slice from pointer + length
unsafe.String(ptr, len)Go 1.20Create string from pointer + length
unsafe.SliceData(s)Go 1.17Get pointer to slice's backing array
unsafe.StringData(s)Go 1.20Get pointer to string's backing array

These are safer than manual uintptr arithmetic because they keep values as pointers (visible to GC) throughout.

weak.Pointer[T] (Go 1.24+)

A weak pointer holds a reference to an object without preventing garbage collection. When the GC reclaims the object, Value() returns nil.

strong := new(MyType)
w := weak.Make(strong)

if p := w.Value(); p != nil {
    // object still alive
} else {
    // object was garbage collected
}
Use Cases
  • Deduplication caches — intern equivalent values without preventing GC

  • Automatic cache eviction — cached objects evict when no strong references remain

runtime.AddCleanup vs runtime.SetFinalizer

Prefer runtime.AddCleanup (Go 1.24+) over runtime.SetFinalizer:

  • Multiple cleanups can be registered per object

  • Cleanup function receives a value, not a pointer to the collected object

  • No risk of resurrecting the object

  • Works correctly with weak pointers

Requirements and compatibility

Written for: Claude Code, OpenAI Codex

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Needs: Declares allowed tools, Claude Code frontmatter, Needs Node.js

The author says: Designed for Claude Code, Codex or similar harness, and for projects using Golang.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
samber
Publisher
Samuel Berthe (Community)
Distributed by
Samuel Berthe
Source
samber/cc-skills-golang · skills/golang-data-structures · commit bb1d43d
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by samber or Samuel Berthe.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download golang-data-structures.zip