{"id":"eb193cdc-996b-41d0-a9d5-d799e865d080","shortId":"bsjuwZ","kind":"skill","title":"go-data-structures","tagline":"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 does","description":"# Go Data Structures\n\n---\n\n## Choosing a Data Structure\n\n```\nWhat do you need?\n├─ Ordered collection of items\n│  ├─ Fixed size known at compile time → Array [N]T\n│  └─ Dynamic size → Slice []T\n│     ├─ Know approximate size? → make([]T, 0, capacity)\n│     └─ Unknown size or nil-safe for JSON? → var s []T (nil)\n├─ Key-value lookup\n│  └─ Map map[K]V\n│     ├─ Know approximate size? → make(map[K]V, capacity)\n│     └─ Need a set? → map[T]struct{} (zero-size values)\n└─ Need to pass to a function?\n   └─ Copy at the boundary if the caller might mutate it\n```\n\n> **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).\n\n---\n\n## Slices\n\n### The append Function\n\n**Always assign the result** — the underlying array may change:\n\n```go\nx := []int{1, 2, 3}\nx = append(x, 4, 5, 6)\n\n// Append a slice to a slice\nx = append(x, y...)  // Note the ...\n```\n\n### Two-Dimensional Slices\n\n**Independent inner slices** (can grow/shrink independently):\n\n```go\npicture := make([][]uint8, YSize)\nfor i := range picture {\n    picture[i] = make([]uint8, XSize)\n}\n```\n\n**Single allocation** (more efficient for fixed sizes):\n\n```go\npicture := make([][]uint8, YSize)\npixels := make([]uint8, XSize*YSize)\nfor i := range picture {\n    picture[i], pixels = pixels[:XSize], pixels[XSize:]\n}\n```\n\n> Read [references/SLICES.md](references/SLICES.md) when debugging unexpected slice behavior, sharing slices across goroutines, or working with slice headers.\n\n### Declaring Empty Slices\n\nPrefer nil slices over empty literals:\n\n```go\n// Good: nil slice\nvar t []string\n\n// Avoid: non-nil but zero-length\nt := []string{}\n```\n\nBoth have `len` and `cap` of zero, but the nil slice is the preferred style.\n\n**Exception for JSON**: A nil slice encodes to `null`, while `[]string{}`\nencodes to `[]`. Use non-nil when you need a JSON array.\n\nWhen designing interfaces, avoid distinguishing between nil and non-nil\nzero-length slices.\n\n---\n\n## Maps\n\n### Implementing a Set\n\nUse `map[T]bool` — idiomatic and reads naturally:\n\n```go\nattended := map[string]bool{\"Ann\": true, \"Joe\": true}\nif attended[person] {  // false if not in map\n    fmt.Println(person, \"was at the meeting\")\n}\n```\n\n---\n\n## Copying\n\nBe careful when copying a struct from another package. If the type has methods\non its pointer type (`*T`), copying the value can cause aliasing bugs.\n\n**General rule:** Do not copy a value of type `T` if its methods are associated\nwith the pointer type `*T`. This applies to `bytes.Buffer`, `sync.Mutex`,\n`sync.WaitGroup`, and types containing them.\n\n```go\n// Bad: copying a mutex\nvar mu sync.Mutex\nmu2 := mu  // almost always a bug\n\n// Good: pass by pointer\nfunc increment(sc *SafeCounter) {\n    sc.mu.Lock()\n    sc.count++\n    sc.mu.Unlock()\n}\n```\n\n---\n\n## Quick Reference\n\n| Topic | Key Point |\n|-------|-----------|\n| Slices | Always assign `append` result; `nil` slice preferred over `[]T{}` |\n| Sets | `map[T]bool` is idiomatic |\n| Copying | Don't copy `T` if methods are on `*T`; beware aliasing |\n\n## Related Skills\n\n- **Defensive copying**: See [go-defensive](../go-defensive/SKILL.md) when copying slices or maps at API boundaries to prevent mutation\n- **Capacity hints**: See [go-performance](../go-performance/SKILL.md) when pre-sizing slices or maps for known workloads\n- **Iteration patterns**: See [go-control-flow](../go-control-flow/SKILL.md) when using range loops over slices, maps, or channels\n- **Declaration style**: See [go-declarations](../go-declarations/SKILL.md) when choosing between `new`, `make`, `var`, and composite literals","tags":["data","structures","golang","skills","cxuu","agent-skills","ai-agent","ai-assistant","claude","claude-code","codex","cursor"],"capabilities":["skill","source-cxuu","skill-go-data-structures","topic-agent-skills","topic-ai-agent","topic-ai-assistant","topic-claude","topic-claude-code","topic-codex","topic-cursor","topic-golang","topic-llm"],"categories":["golang-skills"],"synonyms":[],"warnings":[],"endpointUrl":"https://skills.sh/cxuu/golang-skills/go-data-structures","protocol":"skill","transport":"skills-sh","auth":{"type":"none","details":{"cli":"npx skills add cxuu/golang-skills","source_repo":"https://github.com/cxuu/golang-skills","install_from":"skills.sh"}},"qualityScore":"0.491","qualityRationale":"deterministic score 0.49 from registry signals: · indexed on github topic:agent-skills · 82 github stars · SKILL.md body (3,794 chars)","verified":false,"liveness":"unknown","lastLivenessCheck":null,"agentReviews":{"count":0,"score_avg":null,"cost_usd_avg":null,"success_rate":null,"latency_p50_ms":null,"narrative_summary":null,"summary_updated_at":null},"enrichmentModel":"deterministic:skill-github:v1","enrichmentVersion":1,"enrichedAt":"2026-05-02T12:55:17.820Z","embedding":null,"createdAt":"2026-04-18T22:13:10.143Z","updatedAt":"2026-05-02T12:55:17.820Z","lastSeenAt":"2026-05-02T12:55:17.820Z","tsv":"'/go-concurrency/skill.md':158 '/go-control-flow/skill.md':561 '/go-declarations/skill.md':577 '/go-defensive/skill.md':169,525 '/go-performance/skill.md':181,543 '0':83 '1':198 '2':199 '3':200 '4':204 '5':205 '6':206 'access':147 'across':281 'alias':427,516 'alloc':244 'almost':469 'also':38 'alway':186,470,490 'ann':384 'anoth':410 'api':163,532 'append':20,184,202,207,214,492 'appli':144,450 'approxim':79,106 'array':13,71,192,351 'assign':187,491 'associ':443 'atom':152 'attend':380,389 'avoid':304,355 'bad':460 'behavior':278 'bewar':515 'bool':374,383,502 'boundari':37,132,164,533 'bug':428,472 'build':41 'bytes.buffer':452 'caller':135 'cap':318 'capac':84,112,174,537 'care':404 'caus':426 'chang':194 'channel':570 'choos':14,53,579 'collect':44,62 'compil':69 'composit':585 'concurr':146,157 'contain':457 'control':559 'copi':34,129,161,402,406,422,433,461,505,508,520,527 'data':3,35,51,55,149 'debug':275 'declar':21,288,571,576 'defens':160,168,519,524 'design':353 'dimension':221 'distinguish':356 'dynam':74 'effici':246 'empti':22,289,295 'encod':335,340 'even':45 'except':329 'fals':391 'fix':65,248 'flow':560 'fmt.println':396 'func':477 'function':128,185 'general':429 'go':2,9,50,156,167,179,195,229,250,297,379,459,523,541,558,575 'go-concurr':155 'go-control-flow':557 'go-data-structur':1 'go-declar':574 'go-defens':166,522 'go-perform':178,540 'good':298,473 'goroutin':282 'grow/shrink':227 'header':287 'hint':538 'idiomat':375,504 'implement':29,368 'increment':478 'independ':223,228 'inner':224 'int':197 'interfac':354 'item':64 'iter':554 'joe':386 'json':28,92,331,350 'k':103,110 'key':98,487 'key-valu':97 'know':78,105 'known':67,552 'len':316 'length':311,365 'liter':26,296,586 'lookup':100 'loop':565 'make':18,81,108,231,240,252,256,582 'manipul':43 'map':11,32,101,102,109,116,367,372,381,395,500,530,550,568 'may':193 'meet':401 'method':416,441,511 'might':136 'mu':465,468 'mu2':467 'mutat':137,536 'mutex':151,463 'n':72 'natur':378 'need':60,113,123,348 'new':16,581 'nil':24,89,96,292,299,307,323,333,345,358,362,494 'nil-saf':88 'non':306,344,361 'non-nil':305,343,360 'note':217 'null':337 'oper':153 'order':61 'packag':411 'pass':125,474 'pattern':555 'perform':176,180,542 'person':390,397 'pictur':230,237,238,251,263,264 'pixel':255,266,267,269 'point':488 'pointer':419,446,476 'pre':172,546 'pre-siz':171,545 'prefer':291,327,496 'prevent':535 'quick':484 'rang':236,262,564 'read':271,377 'refer':485 'references/slices.md':272,273 'relat':517 'result':189,493 'rule':430 'safe':90 'safecount':480 'sc':479 'sc.count':482 'sc.mu.lock':481 'sc.mu.unlock':483 'see':154,165,177,521,539,556,573 'set':30,115,370,499 'share':279 'singl':243 'size':66,75,80,86,107,121,173,249,547 'skill':141,518 'skill-go-data-structures' 'slice':10,23,76,182,209,212,222,225,277,280,286,290,293,300,324,334,366,489,495,528,548,567 'source-cxuu' 'string':303,313,339,382 'struct':118,408 'structur':4,52,56,150 'style':328,572 'sync.mutex':453,466 'sync.waitgroup':454 'time':70 'topic':486 'topic-agent-skills' 'topic-ai-agent' 'topic-ai-assistant' 'topic-claude' 'topic-claude-code' 'topic-codex' 'topic-cursor' 'topic-golang' 'topic-llm' 'true':385,387 'two':220 'two-dimension':219 'type':414,420,437,447,456 'uint8':232,241,253,257 'under':191 'unexpect':276 'unknown':85 'use':5,19,39,342,371,563 'user':48 'v':104,111 'valu':99,122,424,435 'var':93,301,464,583 'vs':25 'work':7,284 'workload':553 'x':196,201,203,213,215 'xsize':242,258,268,270 'y':216 'ysize':233,254,259 'zero':120,310,320,364 'zero-length':309,363 'zero-s':119","prices":[{"id":"218ce4b9-b7c1-4c9a-9893-c96bf013efd6","listingId":"eb193cdc-996b-41d0-a9d5-d799e865d080","amountUsd":"0","unit":"free","nativeCurrency":null,"nativeAmount":null,"chain":null,"payTo":null,"paymentMethod":"skill-free","isPrimary":true,"details":{"org":"cxuu","category":"golang-skills","install_from":"skills.sh"},"createdAt":"2026-04-18T22:13:10.143Z"}],"sources":[{"listingId":"eb193cdc-996b-41d0-a9d5-d799e865d080","source":"github","sourceId":"cxuu/golang-skills/go-data-structures","sourceUrl":"https://github.com/cxuu/golang-skills/tree/main/skills/go-data-structures","isPrimary":false,"firstSeenAt":"2026-04-18T22:13:10.143Z","lastSeenAt":"2026-05-02T12:55:17.820Z"}],"details":{"listingId":"eb193cdc-996b-41d0-a9d5-d799e865d080","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"cxuu","slug":"go-data-structures","github":{"repo":"cxuu/golang-skills","stars":82,"topics":["agent-skills","ai-agent","ai-assistant","claude","claude-code","codex","cursor","go","golang","llm"],"license":"apache-2.0","html_url":"https://github.com/cxuu/golang-skills","pushed_at":"2026-03-15T19:32:10Z","description":"AI Agent Skills for idiomatic, production-ready Go code, distilled from Google, Uber, Community","skill_md_sha":"d8a309fda37d4c1c22d61ec181c00ea9c8691e5e","skill_md_path":"skills/go-data-structures/SKILL.md","default_branch":"main","skill_tree_url":"https://github.com/cxuu/golang-skills/tree/main/skills/go-data-structures"},"layout":"multi","source":"github","category":"golang-skills","frontmatter":{"name":"go-data-structures","license":"Apache-2.0","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)."},"skills_sh_url":"https://skills.sh/cxuu/golang-skills/go-data-structures"},"updatedAt":"2026-05-02T12:55:17.820Z"}}