← ClaudeAtlas

go-declarationslisted

Use when declaring or initializing Go variables, constants, structs, or maps. Covers var vs :=, grouped declaration blocks, iota enums starting at 1, struct/map/slice composite literals, raw string literals, `any` over `interface{}`, and avoiding shadowed builtins. Apply proactively to any new struct, const block, or top-level var, even if the user did not ask about declaration style. Does not cover identifier naming (see go-naming).
muratmirgun/gophers · ★ 6 · AI & Automation · score 84
Install: claude install-skill muratmirgun/gophers
# Go Declarations and Initialization Pick the simplest declaration form that expresses your intent: scope variables tightly, group related declarations, and let the zero value do its job. ## Core Rules 1. **`:=` for locals with values; `var` for intentional zero values or top-level declarations.** 2. **Group related declarations in parenthesized blocks.** Separate unrelated ones into distinct blocks. 3. **Start enums at `iota + 1`** so the zero value is "invalid/unset" — unless zero is genuinely meaningful. 4. **Initialize structs with field names.** Omit zero-value fields; let defaults speak. 5. **Use `any`, not `interface{}`,** in all new code. 6. **Never shadow builtins** (`len`, `cap`, `error`, `new`, `make`, `copy`, `any`, `nil`, ...). ## Decision: var vs := | Context | Use | Example | |---|---|---| | Package-level | `var` (always) | `var startTime = time.Now()` | | Local with computed value | `:=` | `s := "foo"` | | Local zero-value, intentional | `var` | `var filtered []int` | | Declared type differs from RHS | `var T = expr` | `var e error = f()` | > Read [references/scope-and-shadowing.md](references/scope-and-shadowing.md) when fighting subtle bugs caused by `:=` redeclaring an outer variable. ## Group Related Declarations ```go // Bad const a = 1 const b = 2 // Good const ( a = 1 b = 2 ) ``` Inside functions, group adjacent vars even if loosely related: ```go var ( caller = c.name format = "json" timeout = 5 * time.Second ) ``` ##