go-code-style
Golang code style, formatting, and conventions. Focuses on clarity, readability, and idiomatic Go patterns.
SKILL.mdskills/go/go-code-style/SKILL.md
GitHubCore Style Principles
- Keep the happy path left — Handle errors and edge cases first with early returns. Avoid deep nesting.
- Short and focused functions — One function should do one thing. Aim for ≤4 parameters.
- := vs var — Use
:=for non-zero initialization,varfor zero-value initialization (signals "this will be set later"). - Initialize slices and maps — Use
[]T{}ormake([]T, 0)instead of leaving them nil to avoid JSON serialization surprises (nullvs[]). - Named Booleans — If a condition has 3+ operands, extract them into named variables for clarity.
- Eliminate unnecessary else — If the
ifblock returns, theelseis redundant. - Use
range— Preferfor i, v := range sliceover index-based loops. - Explicit field names — Always use field names in composite literals (
User{Name: "Alice"}notUser{"Alice"}).
Code Examples
Happy Path Indentation
// ✅ Good
func process(data []byte) error {
if len(data) == 0 {
return ErrEmptyData
}
res, err := parse(data)
if err != nil {
return err
}
return save(res)
}
// ❌ Bad (Deeply nested)
func process(data []byte) error {
if len(data) > 0 {
res, err := parse(data)
if err == nil {
return save(res)
} else {
return err
}
}
return ErrEmptyData
}
Slice and Map Initialization
// ✅ Good
users := []User{} // serializes to []
m := make(map[string]int, len(items))
// ❌ Bad (unless nil is specifically intended)
var users []User // serializes to null
Function Signatures
- Context first:
func Do(ctx context.Context, arg string). - Error last:
func Do(...) (Result, error). - Use pointer receivers (
*T) when mutating or for large structs. - Use value receivers (
T) for small, immutable types.
Formatting
- Use
gofmt(orgofumpt) religiously. - Break lines at semantic boundaries if they exceed ~120 chars.
Anti-patterns
- ❌ Magic numbers: Use named constants instead.
- ❌ Positional struct literals:
User{"Bob", 20}(breaks if fields are added). - ❌ Pointless
else:if err != nil { return err } else { ... }. - ❌ Naked returns in long functions:
return(hard to see what is returned). - ❌ Dot imports:
import . "fmt".