All skills
cxuu avatar

/go-control-flow

@91f0c2e
by Charles Xucxuu/golang-skills165 stars
19

Use when writing conditionals, loops, or switch statements in Go — including if with initialization, early returns, for loop forms, range, switch, type switches, and blank identifier patterns. Also use when writing a simple if/else or for loop, even if the user doesn't mention guard clauses or variable scoping. Does not cover error flow patterns (see go-error-handling).

Use this Skill: https://skilld.dev/gh/cxuu/golang-skills/go-control-flow

This session only. Nothing lands on disk.

referencesSWITCH-PATTERNS.md

≈635 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Switch Patterns

Detailed patterns for Go switch statements, including expression-less switches, comma cases, break behavior, and labeled breaks.


No Automatic Fallthrough

Go switch cases do not fall through by default (unlike C/Java). Each case body implicitly breaks. Use fallthrough only when explicitly needed — it is rare in idiomatic Go.

switch n {
case 1:
    fmt.Println("one")
    // no fallthrough — next case is NOT executed
case 2:
    fmt.Println("two")
}

Expression-less Switch

A switch with no expression switches on true. Use it for clean if-else-if chains when comparing a single variable against multiple conditions:

func unhex(c byte) byte {
    switch {
    case '0' <= c && c <= '9':
        return c - '0'
    case 'a' <= c && c <= 'f':
        return c - 'a' + 10
    case 'A' <= c && c <= 'F':
        return c - 'A' + 10
    }
    return 0
}

Comma-Separated Cases

Multiple values can share a single case body using commas — no need for fallthrough:

func shouldEscape(c byte) bool {
    switch c {
    case ' ', '?', '&', '=', '#', '+', '%':
        return true
    }
    return false
}

Break with Labels

break inside a switch terminates only the switch, not an enclosing for loop. Use a label to break out of the loop:

Loop:
    for n := 0; n < len(src); n += size {
        switch {
        case src[n] < sizeOne:
            break        // breaks switch only
        case src[n] < sizeTwo:
            if n+1 >= len(src) {
                break Loop   // breaks out of for loop
            }
        }
    }

Another common pattern — breaking a range loop from inside a switch:

Loop:
    for _, v := range items {
        switch v.Type {
        case "done":
            break Loop  // breaks the for loop
        case "skip":
            break  // breaks only the switch
        }
    }

Rule of thumb: Whenever you have a switch inside a for and need to exit the loop from a case, always use a labeled break.


Type Switches

For type switches (switch v := x.(type)), see go-interfaces: Type Switch.


Quick Reference

Pattern Syntax
Expression-less switch switch { case cond: }
Comma cases case 'a', 'b', 'c':
No fallthrough Default; use fallthrough keyword if needed
Break switch only break inside case
Break enclosing loop break Label with labeled for

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    The skill provides safe and idiomatic guidelines for writing control flow structures in Go, such as loops, conditionals, switch statements, and blank identifiers. No security risks or malicious patterns were detected.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer6mo

    1 file scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 91f0c2e. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 3 months ago

README badge

README badge for cxuu/golang-skills/go-control-flow