← ClaudeAtlas

go-control-flowlisted

Use when writing conditionals, loops, switches, type switches, or blank-identifier patterns in Go. Covers if-with-initialization, guard clauses, early returns, the unified for loop, range over slices/maps/strings/channels, parallel assignment, labeled break, and `_` for discards and side-effect imports. Apply proactively to any new if/for/switch, even when the user does not mention scoping or shadowing. Does not cover error-flow specifics (see go-error-handling).
muratmirgun/gophers · ★ 6 · AI & Automation · score 84
Install: claude install-skill muratmirgun/gophers
# Go Control Flow Go gives you `if`, `for`, and `switch` — and one looping construct that covers them all. The idioms are small but strict: scope variables tightly, return early, keep the happy path unindented. ## Core Rules 1. **Scope variables with if-init when they live only for the check.** `if x, err := f(); err != nil { ... }`. 2. **Guard clauses over nested `else`.** When the `if` body returns/breaks/continues, drop the `else`. 3. **`:=` redeclares only in the same scope.** In an inner scope it shadows — a frequent bug source. 4. **One `for`, three forms.** Condition-only (while), three-clause (C-style), and infinite (`for {}`). 5. **`range` over string yields runes**, over map yields **non-deterministic order**, over channel **drains until closed**. 6. **`break` inside `switch` only breaks the switch.** Use a labeled `break` to exit the enclosing `for`. 7. **The blank identifier `_` discards, but never errors.** Silent error dropping is a bug. ## Decision: var vs := vs = at a glance | Situation | Use | |---|---| | New variable, scoped to the check | `if v, err := f(); err != nil` | | Reusing an outer variable | plain `=` (avoid `:=` to prevent shadowing) | | At least one new + reuse of outer | `:=` is fine (same scope only) | | Wanted zero value | `var x T` | ## If with Initialization ```go if err := file.Chmod(0664); err != nil { return err } ``` If `err` is needed past the `if`, declare it separately: ```go x, err := f() if err != nil { return err }