Back to Blog

design-creative

Go Data Structures: A Skill for Slices, Maps, and Arrays

A scenario-driven intro to the go-data-structures agent skill: when to use it, why agents benefit, and how to install it.

OpenAgentSkillPublished:

Where this fits

You're building a Go service and your agent just wrote var s []string but the JSON output came back as null instead of []. Or it used new(map[string]int) and panicked on assignment. These are the small, high-frequency decisions that trip up both human developers and AI agents alike. The go-data-structures skill steps in exactly here: it gives your agent a decision tree for choosing between arrays, slices, and maps, and for handling nil vs. empty slices, capacity hints, and boundary copies — without you having to spell out the idioms every time.

Why agents benefit

  • Reduces back‑and‑forth on allocation idioms: The skill encodes the new vs. make distinction, so your agent doesn't guess or ask for clarification.
  • Prevents silent bugs in JSON serialization: It knows when a nil slice is correct (e.g., for omitempty) and when an empty literal is required.
  • Guides capacity planning: When your agent sees a slice that will grow, it can proactively suggest make([]T, 0, cap) based on the skill's reference material.
  • Teaches safe copying at boundaries: The skill reminds the agent to copy slices when passing them to functions that might mutate the caller's data — a classic source of aliasing bugs.
  • Covers set implementation: It points to map[T]struct{} as the idiomatic way to build sets, avoiding the common map[T]bool mistake.

Practical scenarios

Handling JSON responses with nil vs. empty slices

Your agent is writing a REST handler that returns a list of tags. If the list is empty, the JSON should be [], not null. The skill's decision tree tells the agent to use make([]string, 0) when the slice is meant to be serialized, and var s []string when a nil slice is acceptable (e.g., for internal use or with omitempty).

Optimizing a high‑volume map lookup

Your agent is building an in‑memory cache with a map that will hold thousands of entries. Instead of map[string]int{}, the skill suggests make(map[string]int, 1000) to avoid rehashing overhead. It also nudges the agent to consider map[string]struct{} if only keys matter.

Avoiding slice aliasing in a utility function

Your agent writes a function that sorts a slice in place. The skill reminds it to copy the slice at the boundary (append([]T(nil), s...)) so the caller's slice isn't unexpectedly modified. This is the kind of subtle correctness fix that saves hours of debugging.

Add it to your agent workflow

Install the skill with a single command:

npx skills add cxuu/golang-skills --skill go-data-structures

Then, when your agent encounters a Go data structure problem, it will automatically load the skill's reference file (references/SLICES.md) and apply the decision tree. For example, if you ask it to "build a function that returns the unique elements of a slice," the agent will use the skill to decide on a map-based set and handle the empty case correctly.

Compare before adopting

Before you commit, compare this skill with other Go‑specific skills in the OpenAgentSkill ecosystem. Look at:

  • Quality signals: Does the skill have a clear description and a focused scope? (This one explicitly says it does not cover concurrency, which is a good sign.)
  • Maintenance freshness: Check the GitHub repo for recent commits and open issues. A skill that's been stale for a year might miss newer Go idioms.
  • Alternatives: There are skills for go-concurrency and go-errors — decide if you need those too, and whether they integrate well together.
  • Workflow fit: Does the skill's decision tree match how your team writes Go? If you use a framework that wraps slices (like slices package), you may need to adjust.

Why it is worth tracking

With 155 stars and an active author, this skill has traction in the community. Its narrow, well‑defined scope makes it a reliable building block — it doesn't try to cover everything, which means the advice is consistent and tested. Keep an eye on it for updates that might add coverage for generics or the newer slices and maps standard library packages. If you frequently work with Go collections, adding this skill to your agent's toolkit is a low‑risk, high‑payoff move. For a curated list of similar skills, see OpenAgentSkill.

Featured Skill

go-data-structures

Skill by cxuu