100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

switch Statements in Go

Understand Go's switch statement, including implicit break, fallthrough, tagless switches, and type switches.

Control FlowBeginner8 min readJul 8, 2026
Analogies

Introduction

Go's switch statement provides a clean way to compare a value against many possible cases. Unlike C or Java, Go switch cases do not fall through by default: once a matching case runs, control automatically exits the switch. This removes the need for explicit break statements and eliminates a common class of bugs caused by accidentally missing them.

🏏

Cricket analogy: Go's switch is like an umpire's decision tree that stops the moment a verdict is reached, out or not out, without needing to explicitly say stop checking other appeals; unlike older systems, there's no risk of accidentally continuing to the next check.

Syntax

go
switch tag {
case value1:
    // runs if tag == value1
case value2, value3:
    // runs if tag == value2 or value3
default:
    // runs if no case matches
}

// tagless switch, acts like if-else chain
switch {
case x < 0:
    // negative
case x == 0:
    // zero
default:
    // positive
}

// type switch
switch v := i.(type) {
case int:
    // v is an int here
case string:
    // v is a string here
default:
    // v keeps its original type
}

Explanation

Each case in a Go switch implicitly breaks after its statements run, so you never need a break keyword to prevent fall-through. If you deliberately want execution to continue into the next case, you must use the explicit fallthrough keyword. A switch without a tag expression behaves like an if-else-if chain, evaluating each case as a boolean condition top to bottom, which is a common idiom for multi-branch logic. Go also supports type switches, using the special syntax switch v := x.(type), which let you branch based on the dynamic type stored in an interface value.

🏏

Cricket analogy: Each Go switch case implicitly stops, like an umpire's decision ending the appeal automatically; the fallthrough keyword is like the umpire explicitly saying the next check applies too, a tagless switch is like walking through a checklist of yes-or-no appeals top to bottom, and a type switch is like identifying whether a delivery was a no-ball, wide, or legal ball based on its actual type.

Example

go
package main

import "fmt"

func describe(i interface{}) string {
    switch v := i.(type) {
    case int:
        return fmt.Sprintf("int with value %d", v)
    case string:
        return fmt.Sprintf("string of length %d", len(v))
    default:
        return "unknown type"
    }
}

func main() {
    day := 3
    switch day {
    case 1, 7:
        fmt.Println("Weekend")
    case 2, 3, 4, 5, 6:
        fmt.Println("Weekday")
        fallthrough
    default:
        fmt.Println("Processed day", day)
    }

    fmt.Println(describe(42))
    fmt.Println(describe("hello"))
}

Output

go
Weekday
Processed day 3
int with value 42
string of length 5

Key Takeaways

  • Go switch cases break automatically; no explicit break is needed.
  • Use fallthrough to intentionally continue into the next case.
  • A tagless switch works like an if-else-if chain.
  • A type switch uses switch v := x.(type) to branch on dynamic types.
  • Multiple values can be listed in a single case separated by commas.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#SwitchStatementsInGo#Switch#Statements#Syntax#Explanation#StudyNotes#SkillVeris