go-data-structures — Claude Code skill: Go slices, maps, arrays
A skill that steers your coding agent toward correct Go idioms for slices, maps and arrays: make vs new, append, nil slices and JSON.
npx skills add cxuu/golang-skills --skill go-data-structures -a claude-codeWhat go-data-structures does
The skill fires whenever the agent writes or edits Go code that builds collections, even if you never asked about allocation idioms. It ships a decision tree: fixed size means an array [N]T, dynamic size means a slice []T, key lookup means map[K]V, and a set means map[T]struct{}. When the approximate size is known it suggests make([]T, 0, capacity); when JSON needs a real array it suggests var s []T.
A separate references/SLICES.md covers nil versus empty literals, slice copying and capacity. The skill also reminds the agent to always assign the append result, and warns against copying a struct whose methods sit on *T — that bites on bytes.Buffer, sync.Mutex and sync.WaitGroup, where a value copy usually hides an aliasing bug.
Concurrent access is out of scope: for mutexes and atomics the skill points to go-concurrency. Defensive copying at API boundaries lives in go-defensive, and capacity hints in go-performance. Worth checking which of those neighbouring skills you already have installed.
What it helps with
- pick between
makeandvarfor a slice in a JSON response - swap a boolean map for
map[T]struct{}as a set - catch a
sync.Mutexcopied by value - size a 2D slice with a single allocation
- explain to the agent why
appendneeds assignment
Example prompts for the agent
- declare the empty slice so the JSON field encodes as
[]instead ofnull - build a set of attendees with
map[string]struct{} - check this code for aliasing when structs get copied
How to install go-data-structures
1Skills CLI (Vercel)
npx skills add cxuu/golang-skills --skill go-data-structures -a claude-code2Manually: put the files in place
~/.claude/skills/go-data-structures/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/go-data-structures/
1Skills CLI (Vercel)
npx skills add cxuu/golang-skills --skill go-data-structures -a codex2Manually: put the files in place
.agents/skills/go-data-structures/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add cxuu/golang-skills --skill go-data-structures -a cursor2Manually: put the files in place
.cursor/skills/go-data-structures/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install cxuu/golang-skills go-data-structures2Manually: put the files in place
.github/skills/go-data-structures/SKILL.md1Skills CLI (Vercel)
npx skills add cxuu/golang-skills --skill go-data-structures -a gemini-cli2Platform CLI
gemini skills install https://github.com/cxuu/golang-skills --path skills/go-data-structuresManually: put the files in place
.devin/skills/go-data-structures/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add cxuu/golang-skills --skill go-data-structures -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
Step by step, with commands for each platform: How to install skills in Claude Code: five ways
Questions about go-data-structures
How do I install go-data-structures in Claude?
Use the skills CLI: npx skills add cxuu/golang-skills --skill go-data-structures -a claude-code. The skill file then lives at ~/.claude/skills/go-data-structures/SKILL.md.
What does go-data-structures do?
It teaches the agent Go idioms for slices, maps and arrays: choosing between new and make, assigning the append result, nil versus empty slices, sets built on maps, and copying at boundaries.
Does the skill cover concurrent access to data structures?
No. Mutexes and atomic operations are handled by the go-concurrency skill, and defensive copying at API boundaries by go-defensive.
Source text
SKILL.md · Apache-2.0
---
name: go-data-structures
description: Use when working with Go slices, maps, or arrays — choosing between new and make, using append, declaring empty slices (nil vs literal for JSON), implementing sets with maps, and copying data at boundaries. Also use when building or manipulating collections, even if the user doesn't ask about allocation idioms. Does not cover concurrent data structure safety (see go-concurrency).
---
# Go Data Structures
## Resource Routing
- `references/SLICES.md` - Read when deciding nil versus empty slices, copying slices, or managing slice capacity and aliasing.
## Choosing a Data Structure
```
What do you need?
├─ Ordered collection of items
│ ├─ Fixed size known at compile time → Array [N]T
│ └─ Dynamic size → Slice []T
│ ├─ Know approximate size? → make([]T, 0, capacity)
│ └─ Unknown size or nil-safe for JSON? → var s []T (nil)
├─ Key-value lookup
│ └─ Map map[K]V
│ ├─ Know approximate size? → make(map[K]V, capacity)
│ └─ Need a set? → map[T]struct{} (zero-size values)
└─ Need to pass to a function?
└─ Copy at the boundary if the caller might mutate it
```
> **When this skill does NOT apply**: For concurrent access to data structures (mutexes, atomic operations), see [go-concurrency](../go-concurrency/SKILL.md). For defensive copying at API boundaries, see [go-defensive](../go-defensive/SKILL.md). For pre-sizing capacity for performance, see [go-performance](../go-performance/SKILL.md).
---
## Slices
### The append Function
**Always assign the result** — the underlying array may change:
```go
x := []int{1, 2, 3}
x = append(x, 4, 5, 6)
// Append a slice to a slice
x = append(x, y...) // Note the ...
```
### Two-Dimensional Slices
**Independent inner slices** (can grow/shrink independently):
```go
picture := make([][]uint8, YSize)
for i := range picture {
picture[i] = make([]uint8, XSize)
}
```
**Single allocation** (more efficient for fixed sizes):
```go
picture := make([][]uint8, YSize)
pixels := make([]uint8, XSize*YSize)
for i := range picture {
picture[i], pixels = pixels[:XSize], pixels[XSize:]
}
```
### Declaring Empty Slices
Prefer nil slices over empty literals:
```go
// Good: nil slice
var t []string
// Avoid: non-nil but zero-length
t := []string{}
```
Both have `len` and `cap` of zero, but the nil slice is the preferred style.
**Exception for JSON**: A nil slice encodes to `null`, while `[]string{}`
encodes to `[]`. Use non-nil when you need a JSON array.
When designing interfaces, avoid distinguishing between nil and non-nil
zero-length slices.
---
## Maps
### Implementing a Set
Use `map[T]struct{}` when the map is only a set. The empty struct takes no
storage and makes membership intent explicit:
```go
attended := map[string]struct{}{"Ann": {}, "Joe": {}}
if _, ok := attended[person]; ok {
fmt.Println(person, "was at the meeting")
}
```
Use boolean map values only when the value carries a separate meaning beyond
presence.
---
## Copying
Be careful when copying a struct from another package. If the type has methods
on its pointer type (`*T`), copying the value can cause aliasing bugs.
**General rule:** Do not copy a value of type `T` if its methods are associated
with the pointer type `*T`. This applies to `bytes.Buffer`, `sync.Mutex`,
`sync.WaitGroup`, and types containing them.
```go
// Bad: copying a mutex
var mu sync.Mutex
mu2 := mu // almost always a bug
// Good: pass by pointer
func increment(sc *SafeCounter) {
sc.mu.Lock()
sc.count++
sc.mu.Unlock()
}
```
---
## Quick Reference
| Topic | Key Point |
|-------|-----------|
| Slices | Always assign `append` result; `nil` slice preferred over `[]T{}` |
| Sets | `map[T]struct{}` for membership-only sets |
| Copying | Don't copy `T` if methods are on `*T`; beware aliasing |
## Related Skills
- **Defensive copying**: See [go-defensive](../go-defensive/SKILL.md) when copying slices or maps at API boundaries to prevent mutation
- **Capacity hints**: See [go-performance](../go-performance/SKILL.md) when pre-sizing slices or maps for known workloads
- **Iteration patterns**: See [go-control-flow](../go-control-flow/SKILL.md) when using range loops over slices, maps, or channels
- **Declaration style**: See [go-declarations](../go-declarations/SKILL.md) when choosing between `new`, `make`, `var`, and composite literals
© cxuu · Apache-2.0 · cxuu/golang-skills