golang-data-structures

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

1|Updated May 25, 2020
One-click install
npx skills add https://github.com/titaneric/dotfiles --skill golang-data-structures-titaneric
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-data-structures
Source: https://github.com/titaneric/dotfiles/tree/main/dot_agents/skills/golang-data-structures
Command: npx skills add https://github.com/titaneric/dotfiles --skill golang-data-structures-titaneric

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, large-struct copy overhead, and GC pressure from improper pointer use. 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 and slices.Grow, hash bucket structure, and why Go maps never shrink. - Standard Library Containers: Covers container/list, container/heap, container/ring, and bufio with time-complexity tables and selection criteria, plus strings.Builder vs bytes.Buffer guidance. - Generics and Pointer Types: Details constraint selection (comparable, cmp.Ordered), the 6 valid unsafe.Pointer patterns, and weak.Pointer[T] with runtime.AddCleanup for GC-safe caches. - Use Case: When building a priority-queue task scheduler, use this Skill to implement heap.Interface correctly with heap.Fix for O(log n) priority updates instead of remove-and-reinsert. ## Quick Start Ask the agent to review your Go code and recommend the right data structure, for example: should this cache use a value map or pointer map, and how should the slice be preallocated.

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.

When should I use strings.Builder vs bytes.Buffer 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 bidirectional I/O. 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 it.

Should I use a value map or pointer map in Go?

Use map[K]*V for large structs since map access copies the entire value on every read. For small structs under roughly 128 bytes, value maps are typically faster because pointer maps add GC pressure and indirection cost.

When should I not use generics in Go?

Avoid generics for single concrete types, any-constrained functions that just reimplement interface{}, or cases with two or fewer instantiations. Generics add value for containers, algorithms, and utilities where identical logic applies across types, not for serialization helpers.

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

weak.Pointer[T], available since Go 1.24, holds a reference without preventing garbage collection; Value() returns nil after the object is collected. Use it for deduplication caches and canonicalization maps, paired with runtime.AddCleanup instead of the more error-prone SetFinalizer.