{"id":"e38e3f35-3995-4777-bf1d-0b9b11ce9fbe","shortId":"p6qxZB","kind":"skill","title":"go-declarations","tagline":"Use when declaring or initializing Go variables, constants, structs, or maps — including var vs :=, reducing scope with if-init, formatting composite literals, designing iota enums, and using any instead of interface{}. Also use when writing a new struct or const block, even if t","description":"# Go Declarations and Initialization\n\n---\n\n## Quick Reference: var vs :=\n\n| Context | Use | Example |\n|---------|-----|---------|\n| Top-level | `var` (always) | `var _s = F()` |\n| Local with value | `:=` | `s := \"foo\"` |\n| Local zero-value (intentional) | `var` | `var filtered []int` |\n| Type differs from expression | `var` with type | `var _e error = F()` |\n\n> Read [references/SCOPE.md](references/SCOPE.md) when deciding between var and := in complex initialization patterns or multi-return assignments.\n\n---\n\n## Group Similar Declarations\n\nGroup related `var`, `const`, `type` in parenthesized blocks. Separate\n**unrelated** declarations into distinct blocks.\n\n```go\n// Bad\nconst a = 1\nconst b = 2\n\n// Good\nconst (\n    a = 1\n    b = 2\n)\n```\n\nInside functions, group adjacent vars even if unrelated:\n\n```go\nvar (\n    caller  = c.name\n    format  = \"json\"\n    timeout = 5 * time.Second\n)\n```\n\n---\n\n## Constants and iota\n\nStart enums at one so the zero value represents invalid/unset:\n\n```go\nconst (\n    Add Operation = iota + 1\n    Subtract\n    Multiply\n)\n```\n\nUse zero when the default behavior is desirable (e.g., `LogToStdout`).\n\n> Read [references/IOTA.md](references/IOTA.md) when designing iota enums with bitmask patterns, byte-size constants, or String() methods.\n\n---\n\n## Variable Scope\n\nUse if-init to limit scope when the result is only needed for the error check:\n\n```go\nif err := os.WriteFile(name, data, 0644); err != nil {\n    return err\n}\n```\n\nDon't reduce scope if it forces deeper nesting or you need the result outside\nthe `if`. Move constants into functions when only used there.\n\n> Read [references/SCOPE.md](references/SCOPE.md) when working with top-level declarations or choosing between var and := for local variables.\n\n---\n\n## Initializing Structs\n\n- **Always use field names** (enforced by `go vet`). Exception: test tables\n  with ≤3 fields.\n- **Omit zero-value fields** — let Go set defaults.\n- **Use `var` for zero-value structs**: `var user User` not `user := User{}`\n- **Use `&T{}` over `new(T)`**: `sptr := &T{Name: \"bar\"}`\n\n> Read [references/STRUCTS.md](references/STRUCTS.md) when initializing structs with many fields, building slices of struct pointers, or choosing single-line vs multi-line format.\n\n---\n\n## Composite Literal Formatting\n\nUse field names for external package types. Match closing brace indentation\nwith the opening line. Omit repeated type names in slice/map literals\n(`gofmt -s`).\n\n> Read [references/INITIALIZATION.md](references/INITIALIZATION.md) when working with complex composite literals, cuddled braces, or zero-value field decisions.\n\n> Read [references/LITERALS.md](references/LITERALS.md) when formatting complex composite literals.\n\n---\n\n## Initializing Maps\n\n| Scenario | Use | Example |\n|----------|-----|---------|\n| Empty, populated later | `make(map[K]V)` | `m := make(map[string]int)` |\n| Nil declaration | `var` | `var m map[string]int` |\n| Fixed entries at init | Literal | `m := map[string]int{\"a\": 1}` |\n\n`make()` visually distinguishes empty-but-initialized from nil. Use size hints\nwhen the count is known.\n\n---\n\n## Raw String Literals\n\nUse backtick strings to avoid hand-escaped characters:\n\n```go\n// Bad\nwantError := \"unknown name:\\\"test\\\"\"\n\n// Good\nwantError := `unknown name:\"test\"`\n```\n\nIdeal for regex, SQL, JSON, and multi-line text.\n\n---\n\n## Prefer `any` Over `interface{}`\n\nGo 1.18+: use `any` instead of `interface{}` in all new code.\n\n---\n\n## Avoid Shadowing Built-In Names\n\nNever use predeclared identifiers (`error`, `string`, `len`, `cap`, `append`,\n`copy`, `new`, `make`, `close`, `delete`, `panic`, `recover`, `any`, `true`,\n`false`, `nil`, `iota`) as names. Use `go vet` to detect.\n\n```go\n// Bad — shadows the builtin\nvar error string\n\n// Good\nvar errorMessage string\n```\n\n> Read [references/SHADOWING.md](references/SHADOWING.md) when debugging issues where := creates new variables that shadow outer scope.\n\n---\n\n## Related Skills\n\n- **Naming conventions**: See [go-naming](../go-naming/SKILL.md) when choosing variable names, constant names, or deciding name length by scope\n- **Data structures**: See [go-data-structures](../go-data-structures/SKILL.md) when choosing between `new` and `make`, or initializing slices and maps\n- **Control flow scoping**: See [go-control-flow](../go-control-flow/SKILL.md) when using if-init, `:=` redeclaration, or avoiding variable shadowing\n- **Capacity hints**: See [go-performance](../go-performance/SKILL.md) when pre-allocating maps or slices with known sizes","tags":["declarations","golang","skills","cxuu","agent-skills","ai-agent","ai-assistant","claude","claude-code","codex","cursor","llm"],"capabilities":["skill","source-cxuu","skill-go-declarations","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-declarations","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 (4,502 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.876Z","embedding":null,"createdAt":"2026-04-18T22:13:10.952Z","updatedAt":"2026-05-02T12:55:17.876Z","lastSeenAt":"2026-05-02T12:55:17.876Z","tsv":"'/go-control-flow/skill.md':611 '/go-data-structures/skill.md':591 '/go-naming/skill.md':571 '/go-performance/skill.md':628 '0644':231 '1':131,138,176,437 '1.18':493 '2':134,140 '3':293 '5':156 'add':173 'adjac':144 'alloc':632 'also':36 'alway':64,281 'append':517 'assign':109 'avoid':462,503,619 'b':133,139 'backtick':459 'bad':128,468,538 'bar':325 'behavior':184 'bitmask':197 'block':45,120,126 'brace':362,387 'build':335 'built':506 'built-in':505 'builtin':541 'byte':200 'byte-s':199 'c.name':152 'caller':151 'cap':516 'capac':622 'charact':466 'check':224 'choos':272,341,573,593 'close':361,521 'code':502 'complex':102,383,399 'composit':25,350,384,400 'const':44,116,129,132,136,172 'constant':11,158,202,254,576 'context':57 'control':603,609 'convent':566 'copi':518 'count':452 'creat':556 'cuddl':386 'data':230,584,589 'debug':553 'decid':97,579 'decis':393 'declar':3,6,50,112,123,270,420 'deeper':243 'default':183,303 'delet':522 'design':27,193 'desir':186 'detect':536 'differ':83 'distinct':125 'distinguish':440 'e':90 'e.g':187 'empti':407,442 'empty-but-initi':441 'enforc':285 'entri':428 'enum':29,162,195 'err':227,232,235 'error':91,223,513,543 'errormessag':547 'escap':465 'even':46,146 'exampl':59,406 'except':289 'express':85 'extern':357 'f':67,92 'fals':527 'field':283,294,299,334,354,392 'filter':80 'fix':427 'flow':604,610 'foo':72 'forc':242 'format':24,153,349,352,398 'function':142,256 'go':2,9,49,127,149,171,225,287,301,467,492,533,537,569,588,608,626 'go-control-flow':607 'go-data-structur':587 'go-declar':1 'go-nam':568 'go-perform':625 'gofmt':375 'good':135,473,545 'group':110,113,143 'hand':464 'hand-escap':463 'hint':449,623 'ideal':478 'identifi':512 'if-init':21,209,614 'includ':15 'indent':363 'init':23,211,430,616 'initi':8,52,103,279,330,402,444,599 'insid':141 'instead':33,496 'int':81,418,426,435 'intent':77 'interfac':35,491,498 'invalid/unset':170 'iota':28,160,175,194,529 'issu':554 'json':154,482 'k':412 'known':454,637 'later':409 'len':515 'length':581 'let':300 'level':62,269 'limit':213 'line':344,348,367,486 'liter':26,351,374,385,401,431,457 'local':68,73,277 'logtostdout':188 'm':414,423,432 'make':410,415,438,520,597 'mani':333 'map':14,403,411,416,424,433,602,633 'match':360 'method':205 'move':253 'multi':107,347,485 'multi-lin':346,484 'multi-return':106 'multipli':178 'name':229,284,324,355,371,471,476,508,531,565,570,575,577,580 'need':220,247 'nest':244 'never':509 'new':41,320,501,519,557,595 'nil':233,419,446,528 'omit':295,368 'one':164 'open':366 'oper':174 'os.writefile':228 'outer':561 'outsid':250 'packag':358 'panic':523 'parenthes':119 'pattern':104,198 'perform':627 'pointer':339 'popul':408 'pre':631 'pre-alloc':630 'predeclar':511 'prefer':488 'quick':53 'raw':455 'read':93,189,261,326,377,394,549 'recov':524 'redeclar':617 'reduc':18,238 'refer':54 'references/initialization.md':378,379 'references/iota.md':190,191 'references/literals.md':395,396 'references/scope.md':94,95,262,263 'references/shadowing.md':550,551 'references/structs.md':327,328 'regex':480 'relat':114,563 'repeat':369 'repres':169 'result':217,249 'return':108,234 'scenario':404 'scope':19,207,214,239,562,583,605 'see':567,586,606,624 'separ':121 'set':302 'shadow':504,539,560,621 'similar':111 'singl':343 'single-lin':342 'size':201,448,638 'skill':564 'skill-go-declarations' 'slice':336,600,635 'slice/map':373 'source-cxuu' 'sptr':322 'sql':481 'start':161 'string':204,417,425,434,456,460,514,544,548 'struct':12,42,280,310,331,338 'structur':585,590 'subtract':177 'tabl':291 'test':290,472,477 'text':487 'time.second':157 'timeout':155 'top':61,268 'top-level':60,267 'topic-agent-skills' 'topic-ai-agent' 'topic-ai-assistant' 'topic-claude' 'topic-claude-code' 'topic-codex' 'topic-cursor' 'topic-golang' 'topic-llm' 'true':526 'type':82,88,117,359,370 'unknown':470,475 'unrel':122,148 'use':4,31,37,58,179,208,259,282,304,317,353,405,447,458,494,510,532,613 'user':312,313,315,316 'v':413 'valu':70,76,168,298,309,391 'var':16,55,63,65,78,79,86,89,99,115,145,150,274,305,311,421,422,542,546 'variabl':10,206,278,558,574,620 'vet':288,534 'visual':439 'vs':17,56,345 'wanterror':469,474 'work':265,381 'write':39 'zero':75,167,180,297,308,390 'zero-valu':74,296,307,389","prices":[{"id":"f2c20e6d-c7bd-4c9a-8b85-b145a5a8ac98","listingId":"e38e3f35-3995-4777-bf1d-0b9b11ce9fbe","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.952Z"}],"sources":[{"listingId":"e38e3f35-3995-4777-bf1d-0b9b11ce9fbe","source":"github","sourceId":"cxuu/golang-skills/go-declarations","sourceUrl":"https://github.com/cxuu/golang-skills/tree/main/skills/go-declarations","isPrimary":false,"firstSeenAt":"2026-04-18T22:13:10.952Z","lastSeenAt":"2026-05-02T12:55:17.876Z"}],"details":{"listingId":"e38e3f35-3995-4777-bf1d-0b9b11ce9fbe","quickStartSnippet":null,"exampleRequest":null,"exampleResponse":null,"schema":null,"openapiUrl":null,"agentsTxtUrl":null,"citations":[],"useCases":[],"bestFor":[],"notFor":[],"kindDetails":{"org":"cxuu","slug":"go-declarations","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":"201887111b70a29cd84a3697fb079125e6375b75","skill_md_path":"skills/go-declarations/SKILL.md","default_branch":"main","skill_tree_url":"https://github.com/cxuu/golang-skills/tree/main/skills/go-declarations"},"layout":"multi","source":"github","category":"golang-skills","frontmatter":{"name":"go-declarations","license":"Apache-2.0","description":"Use when declaring or initializing Go variables, constants, structs, or maps — including var vs :=, reducing scope with if-init, formatting composite literals, designing iota enums, and using any instead of interface{}. Also use when writing a new struct or const block, even if the user doesn't ask about declaration style. Does not cover naming conventions (see go-naming).","compatibility":"Uses any keyword (Go 1.18+)"},"skills_sh_url":"https://skills.sh/cxuu/golang-skills/go-declarations"},"updatedAt":"2026-05-02T12:55:17.876Z"}}