go-declarations

Solid

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).

AI & Automation 8 stars 1 forks Updated 1 weeks ago MIT

Install

View on GitHub

Quality Score: 82/100

Stars 20%
32
Recency 20%
90
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
80
License 10%
100
Description 5%
100

Skill Content

# 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 ) ``` ## ...

Details

Author
muratmirgun
Repository
muratmirgun/gophers
Created
2 months ago
Last Updated
1 weeks ago
Language
Python
License
MIT

Similar Skills

Semantically similar based on skill content — not just same category