cxuu avatar

Go Control Flow

Go conditionals, loops, and switch statements with guard clauses, scoped initialization, and blank identifier patterns.

skills.sh installs
1.3K
GitHub stars
196
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers if with initialization, early-return guard clauses, and the for, range, and switch forms in Go
  • Includes references/SWITCH-PATTERNS.md for type switches, fallthrough, and labeled breaks, plus references/BLANK-IDENTIFIER.md for discards and imports
  • Warns about := shadowing in inner scopes and shows when = should be used to avoid silent bugs
  • Designed for Go code where error handling is left to the go-error-handling skill and interface checks to go-interfaces

Install

$ npx -y skills add cxuu/golang-skills --skill go-control-flow -y

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

Files

Go Control Flow

Resource Routing

  • references/SWITCH-PATTERNS.md - Read when using switch statements, type switches, fallthrough, or labeled breaks.
  • references/BLANK-IDENTIFIER.md - Read when using _, blank imports, unused compile-time assertions, or intentional discards.

If with Initialization

if and switch accept an optional initialization statement. Use it to scope variables to the conditional block:

if err := file.Chmod(0664); err != nil {
    log.Print(err)
    return err
}

If you need the variable beyond a few lines after the if, declare it separately and use a standard if instead:

x, err := f()
if err != nil {
    return err
}
// lots of code that uses x

Indent Error Flow (Guard Clauses)

When an if body ends with break, continue, goto, or return, omit the unnecessary else. Keep the success path unindented:

f, err := os.Open(name)
if err != nil {
    return err
}
d, err := f.Stat()
if err != nil {
    f.Close()
    return err
}
codeUsing(f, d)

Never bury normal flow inside an else when the if already returns.


Redeclaration and Reassignment

Details

Repository
github.com/cxuu/golang-skills
Category
Go
First seen
May 16, 2026

More from cxuu/golang-skills

All 20

Similar to Go Control Flow

More Go