switch Statements in Go
Tagged and tagless switch, fallthrough
switch Basics
Go's switch is more powerful than in C: no fall-through by default, and cases can have multiple values:
day := "Monday"
switch day {
case "Saturday", "Sunday":
fmt.Println("Weekend")
default:
fmt.Println("Weekday")
}No Implicit Fallthrough
In Go, each case body breaks automatically — unlike C where you must write break explicitly:
- Cleaner code, fewer bugs
- Use
fallthroughexplicitly when you need C-like behavior (rare)
All lessons in this course
- if Statements and Init Expressions
- The for Loop: All Patterns
- switch Statements in Go
- break, continue and Labels