go-data-structures

Guides selection and optimization of Go data structures based on memory internals and access patterns.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-data-structures-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-data-structures
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/go/go-data-structures
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-data-structures-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Choosing the wrong Go data structure causes hidden allocation, reallocation, and copying costs that only surface under load. This Skill helps you select slices, maps, arrays, container packages, and pointer types based on their internals rather than familiarity. ## Core Features & Use Cases - Internals-Based Selection: Explains slice header layout, map bucket growth, and array value semantics so you pick the right structure for each access pattern. - Preallocation & Growth Discipline: Enforces make([]T, 0, n), make(map[K]V, n), and slices.Grow to eliminate repeated growth copies and rehashing. - Container & Pointer Guidance: Covers container/list, container/heap, container/ring, bufio, strings.Builder vs bytes.Buffer, generic constraints, unsafe.Pointer spec patterns, and weak.Pointer[T] for GC-safe caches. - Use Case: When auditing a hot loop that appends to a slice without preallocation or stores large structs as map values, use this Skill to identify the copy overhead and apply the correct fix. ## Quick Start Ask the AI to review your Go code for data structure choices and recommend preallocation, container, or pointer improvements based on memory internals.

Frequently Asked Questions about go-data-structures

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I preallocate slices and maps in Go?

Use make([]T, 0, n) for slices and make(map[K]V, n) for maps when the size is known or estimable. For bulk appends, slices.Grow (Go 1.21+) pre-grows capacity before the loop, eliminating repeated backing-array copies and map rehashing.

When should I use strings.Builder vs bytes.Buffer in Go?

Use strings.Builder for pure string building because its String() method avoids a copy. Use bytes.Buffer when you need io.Reader/io.Writer interfaces or byte manipulation. Both support Grow(n) for preallocation.

When should I use container/list instead of a slice in Go?

Use container/list only for frequent middle insertions and removals, such as LRU caches needing O(1) move-to-front. Slices outperform linked lists for most cases due to cache locality, so benchmark before choosing a linked list.

Is it safe to store unsafe.Pointer as uintptr in Go?

No. Storing a pointer as uintptr across statements is dangerous because the GC can move the object between statements, leaving a dangling pointer. Keep values as typed pointers and use unsafe.Add or unsafe.Slice for arithmetic instead.

Should Go maps store large structs as values or pointers?

Store large structs as map[K]*V because map access copies the entire value per lookup. For small structs under roughly 128 bytes, value maps are typically faster since pointer maps add GC pressure.

What is weak.Pointer in Go and when should I use it?

weak.Pointer[T] (Go 1.24+) holds a reference without preventing garbage collection; Value() returns nil after the object is reclaimed. Use it for deduplication caches and canonicalization maps where entries should evict automatically.