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
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
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
Weekday
Processed day 3
int with value 42
string of length 5Key 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
1. What happens after a matching case in a Go switch finishes executing?
2. How do you intentionally continue execution into the next case in Go?
3. What does a switch statement with no tag expression behave like?
4. What syntax is used for a Go type switch?
5. Can a single case in a Go switch match multiple values?
Was this page helpful?
You May Also Like
if-else Statements in Go
Learn how Go's if-else statement works without parentheses, including its unique init-statement syntax.
Type Assertions and Type Switches in Go
Learn how to safely extract and branch on the concrete type stored inside a Go interface value.
Interfaces in Go
Learn how Go interfaces define behavior through implicit, structural typing without classes or inheritance.
for Loops in Go
Master Go's single looping construct, which covers classic for, while-style, infinite, and range-based loops.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics