golang-data-structures

Guides selection and optimization of Go slices, maps, arrays, containers, and pointer types.

Updated Jun 15, 2026
One-click install
npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-data-structures-2877389577
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: golang-data-structures
Source: https://github.com/2877389577/novels_ai_gen/tree/main/.agents/skills/golang-data-structures
Command: npx skills add https://github.com/2877389577/novels_ai_gen --skill golang-data-structures-2877389577

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Choosing the wrong Go data structure leads to hidden allocation costs, memory bloat, and subtle bugs like append aliasing or maps that never shrink. This Skill provides internals-level guidance on slices, maps, arrays, container packages, generics, and pointer types so Go code uses the right structure with correct preallocation and copy semantics. ## Core Features & Use Cases - Slice and Map Internals: Explains capacity growth, preallocation with make and slices.Grow, hash bucket behavior, and why Go maps never shrink after deletion. - Container and Builder Selection: Covers container/list, container/heap, container/ring, bufio, and the strings.Builder vs bytes.Buffer decision with complexity tables. - Generics and Pointer Types: Guides constraint selection (comparable, cmp.Ordered), the 6 valid unsafe.Pointer patterns, and weak.Pointer with runtime.AddCleanup for GC-friendly caches. - Use Case: When building a priority-queue task scheduler, an LRU cache, or a string interning table, this Skill directs the AI to heap.Fix, container/list with element references, or weak.Pointer instead of naive re-sorting or finalizer-based approaches. ## Quick Start Ask the AI to review or write Go code involving slices, maps, containers, generics, or unsafe/weak pointers, for example: write a generic Set type and explain whether to preallocate the underlying map.

Frequently Asked Questions about golang-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. Preallocation avoids repeated backing-array copies during slice growth and rehashing during map population. Go 1.21+ also offers slices.Grow for pre-growing before bulk appends.

strings.Builder vs bytes.Buffer: which should I use in Go?▼

Use strings.Builder for pure string concatenation because String() returns the result without copying. Use bytes.Buffer when you need io.Reader/io.Writer behavior or byte manipulation. Both support Grow(n) for preallocation.

Why doesn't my Go map release memory after deleting entries?▼

Go maps never shrink their bucket array; delete() removes entries but the allocated buckets are retained. The only fix is rebuilding: create a fresh map with make and copy surviving entries, or set the map to nil and reassign so the GC can reclaim it.

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

Use container/list only when you need O(1) insertion or removal at arbitrary positions via element references, such as LRU caches. Slices outperform linked lists for most cases due to cache locality, since each list node is a separate heap allocation.

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 collected. Use it for caches and canonicalization maps, paired with runtime.AddCleanup instead of the more error-prone runtime.SetFinalizer.

When should I avoid using generics in Go?▼

Avoid generics when the constraint is just any (it adds syntax over interface{} with no benefit), when only one or two instantiations exist, or when constraints become convoluted. Generics shine for containers, algorithms, and utilities where logic is identical across types.