Go switch Statement
The switch statement selects one of several code blocks to run, based on the value of an expression. It's a cleaner, more readable alternative to a long chain of if...else if statements when you're comparing one value against several possibilities.
Go's switch has a notable difference from C, C++, Java, or JavaScript: it only runs the first matching case and then stops automatically — there is no fall-through by default, and no break statement is needed.
Basic Syntax
switch expression {
case value1:
// code block
case value2:
// code block
case value3:
// code block
default:
// code block (optional)
}
How switch Works
- The expression is evaluated exactly once.
- Its result is compared against each
casevalue, in order. - When a match is found, that block runs, and the
switchstatement ends immediately — no other cases are checked. - If no case matches, the optional
defaultblock runs.
Example: Displaying a Day Name
package main
import "fmt"
func main() {
dayNumber := 5
switch dayNumber {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
case 7:
fmt.Println("Sunday")
}
}
Expected output:
Friday
Example: Using the default Case
default acts as a fallback when no other case matches — including it is optional, but strongly recommended so unexpected values don't pass through silently.
package main
import "fmt"
func main() {
score := 105
switch score {
case 0, 1, 2, 3, 4:
fmt.Println("Very Low Score")
case 5, 6, 7, 8:
fmt.Println("Average Score")
case 9, 10:
fmt.Println("Excellent Score")
default:
fmt.Println("Invalid score")
}
}
Expected output:
Invalid score
Key Features of Go's switch
- No
breakneeded — each case terminates automatically once its block finishes. - A single
casecan list multiple comma-separated values, all mapping to the same block. defaultis optional but improves robustness by handling unanticipated values explicitly.- Reads more cleanly than an equivalent chain of
if...else ifstatements once you have more than two or three branches.
If you do want fall-through: Go supports it explicitly, with the
fallthroughkeyword placed at the end of a case block. This is rarely used, and it's worth knowing it exists mainly so you recognize it if you see it — most Go code never needs it, precisely because each case not falling through by default is usually the behavior you want.
Multi-Value Cases
A single case can match multiple values by separating them with commas, letting you group related values without duplicating code.
switch expression {
case value1, value2:
// runs if expression equals value1 OR value2
case value3, value4:
// runs if expression equals value3 OR value4
default:
// runs if nothing matches
}
Example: Categorizing Days of the Week
package main
import "fmt"
func main() {
day := 6
switch day {
case 1, 3, 5:
fmt.Println("Weekday: Odd schedule")
case 2, 4:
fmt.Println("Weekday: Even schedule")
case 6, 7:
fmt.Println("Weekend: Time to relax")
default:
fmt.Println("Invalid day number")
}
}
Expected output:
Weekend: Time to relax
Example: Grouping User Roles
package main
import "fmt"
func main() {
role := "editor"
switch role {
case "admin", "superuser":
fmt.Println("Full system access granted")
case "editor", "author":
fmt.Println("Content editing permissions granted")
case "viewer", "guest":
fmt.Println("Read-only access")
default:
fmt.Println("Unknown role")
}
}
Expected output:
Content editing permissions granted
Switch Without an Expression
Go also allows a switch with no expression at all, in which case each case is itself a boolean condition — this form works like a cleaner if...else if chain:
package main
import "fmt"
func main() {
hour := 14
switch {
case hour < 12:
fmt.Println("Good morning!")
case hour < 18:
fmt.Println("Good afternoon!")
default:
fmt.Println("Good evening!")
}
}
This form is common in real Go code whenever the branches depend on different conditions rather than the same variable being compared to fixed values.
Common Mistakes
- Expecting fall-through by default, as in C or Java — Go's
switchstops after the first match unless you explicitly writefallthrough. - Forgetting the
defaultcase and being surprised when an unexpected value produces no output at all, rather than an error — aswitchwith no matching case and nodefaultsimply does nothing.