golang-data-structures

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

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Choosing the wrong Go data structure causes hidden performance costs: repeated slice growth copies, map rehashing, unnecessary struct copies on map access, and GC pressure from poor pointer choices. This Skill provides internals-level guidance so you pick the right structure based on memory layout, allocation cost, and access patterns. ## Core Features & Use Cases - Slice and Map Internals: Explains capacity growth mechanics, preallocation with make(T, 0, n), the slices and maps packages (Go 1.21+), and why Go maps never shrink their bucket arrays. - Standard Library Containers: Covers container/list, container/heap, container/ring, and bufio, including when linked lists lose to slices due to cache locality. - Generics and Pointers: Guidance on tight constraints (comparable, cmp.Ordered), the 6 valid unsafe.Pointer patterns, and weak.Pointer[T] with runtime.AddCleanup for GC-friendly caches (Go 1.24+). - Use Case: When building a priority-queue task scheduler, an LRU cache, or a string interning table, this Skill tells you exactly which structure, constraint, and copy semantics to use. ## Quick Start Ask the AI to help you choose or implement the right Go data structure for your task, such as writing a generic Set or diagnosing why a map's memory never shrinks after deletions.

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 for better performance?▼

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 O(n) 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 or io.Writer interfaces for I/O operations. 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 bucket memory is 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 the old one.

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

Use container/list only when you need O(1) insertion or removal at arbitrary positions with stable 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 once the object is collected. Use it for deduplication 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 for single concrete types, any-constrained functions that just reimplement interface{} (like JSON marshaling helpers), or abstractions with two or fewer instantiations. Generics add value for containers, algorithms, and utilities where identical logic spans multiple types.