cxuu avatar

Go Control Flow

This covers the full spectrum of Go's control flow constructs: if with initialization, guard clauses with early returns, the three forms of for loops, range iteration, switch statements, and the blank identifier. The guide is opinionated about indentation (keep success paths unindented, bury errors not normal flow) and warns about the shadowing trap with short declarations inside blocks. It links out to separate references for switch patterns and blank identifier details, and explicitly excludes error flow patterns, sending you to go-error-handling instead. The quick reference table at the end is a solid cheat sheet for the non-obvious bits like labeled breaks and compile-time interface checks.

skills.sh installs
1.3K
GitHub stars
193
last updated
Aug 21, 2026

Install

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

Installs into .claude/skills of the current project.

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

Sponsors

Details

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

More from cxuu/golang-skills

All 20
1neolabhq avatarapply-anthropic-skill-best-practicesneolabhq/context-engineering-kitComprehensive guide for skill development based on Anthropic's official best practices - use for complex skills requiring detailed structure1.2Kskills.sh installs
2davila7 avatarsenior-qadavila7/claude-code-templatesComprehensive QA and testing skill for quality assurance, test automation, and testing strategies for ReactJS, NextJS, NodeJS applications. Includes test suite 1.2Kskills.sh installs
3jasonjgardner avatarblockbench-mcp-overviewjasonjgardner/blockbench-mcp-projectOverview of the Blockbench MCP server tools, resources, and prompts. Use to understand the full MCP capability set, learn how tools work together, or when start1.2Kskills.sh installs
4secondsky avatarsap-cap-capiresecondsky/sap-skillsSAP Cloud Application Programming Model (CAP) development skill using Capire documentation. Use when: building CAP applications, defining CDS models, implementi1.2Kskills.sh installs
5nexscope-ai avataramazon-product-compliancenexscope-ai/amazon-skillsProduct compliance and safety — certifications, labeling requirements, restricted substances, documentation1.2Kskills.sh installs
6erikote04 avatarswift-api-design-guidelines-skillerikote04/swift-api-design-guidelines-agent-skillWrite, review, or improve Swift APIs using Swift API Design Guidelines for naming, argument labels, documentation comments, terminology, and general conventions1.2Kskills.sh installs