<< All versions

Skill v1.0.0

currentAutomated scan100/100
reagin/agent-skills/golang-data-structures
──Details
PublishedOctober 3, 2026 at 02:26 PM
Content Hashsha256:5c7dd1a5f8e12744...
Git SHA
──Files
Files (1 file, 4.3 KB)
SKILL.md4.3 KBactive
SKILL.md · 64 lines · 4.3 KB

version: "1.0.0" name: golang-data-structures description: Choose or review Go collections and memory representations when access patterns, ownership, copying, allocation, or generic APIs matter. Use for slices, maps, arrays, standard containers, pointers, and custom generic structures; not for synchronization design or speculative micro-optimization. license: MIT


Go Data Structures

Choose the simplest representation that satisfies the operation, ownership, and lifecycle requirements. Internal runtime layouts and growth policies are implementation details unless the current toolchain documents them as API.

Gather requirements

Before changing a representation, inspect nearby code, go.mod, tests, serialization contracts, concurrency, and performance evidence. Determine:

  • dominant operations and their required complexity;
  • expected and worst-case size;
  • insertion, deletion, and ordering requirements;
  • uniqueness and key equality rules;
  • mutation and ownership across API boundaries;
  • whether nil, empty, and absent have distinct external meaning;
  • whether the structure is shared concurrently;
  • whether allocation or retention is a measured concern.

Do not replace a familiar built-in type with a custom container solely for theoretical complexity at small sizes.

Selection guide

NeedStarting pointImportant trade-off
Ordered, indexable sequenceSliceAppend can replace the backing array; subslices can retain or alias data
Fixed-size comparable valueArrayAssignment copies the full value; size is part of the type
Keyed lookupMapIteration order is unspecified; map access is not safe with concurrent writes
Set membershipmap[T]struct{} or project typeDecide whether a named abstraction improves the API
Priority queuecontainer/heapInterface adapter and heap invariant
O(1) removal with a retained node handlecontainer/listAllocation and poor locality often lose to slices otherwise
Fixed circular traversalcontainer/ring or a slice-based ringChoose based on indexing, storage, and mutation needs
String constructionstrings.BuilderBuilder is write-only and must not be copied after use
Read/write byte bufferbytes.BufferExposes byte-oriented I/O and mutable storage

Read container choices when a standard container or buffering API is under consideration.

Stable semantics

  • Preallocate a slice or map when a credible size estimate exists and the allocation matters. Do not reserve a large worst case without evidence.
  • Treat slice assignment, reslicing, and map assignment as sharing underlying state. Clone at ownership boundaries when independent mutation is required.
  • Treat nil and empty according to the API contract. Both can be idiomatic; they differ in some encodings and in whether a map can be written.
  • Do not depend on a particular slice growth factor, map bucket layout, or whether deleted map storage is returned to the runtime.
  • Map iteration order must not drive deterministic output. Collect and sort keys when order is part of the contract.
  • A value stored in a map cannot be addressed directly. Copy-modify-store or use pointers when identity and mutation semantics justify them.

See slice semantics and map semantics for aliasing, retention, and copying decisions.

Generics and pointers

Use generics when one algorithm or representation is genuinely identical across types and the constraint expresses the required operations. Prefer a concrete type or interface when behavior differs by domain. See generic collections.

Choose values or pointers based on identity, mutation, optionality, copying, allocation, and API contracts—not a universal byte threshold. Use unsafe only for a demonstrated low-level requirement and verify against the current toolchain documentation. See pointer choices.

Verification

Add tests for observable semantics: ordering, duplicate handling, nil/empty behavior, aliasing, boundary ownership, and error cases. Benchmark only when the choice is performance-sensitive under representative sizes. If concurrency is involved, design synchronization separately; changing the container does not make an ownership protocol safe.

All versions