The switch Statement
In this page:
Basic switch
A switch compares an expression against a series of case values and runs the matching branch's code, then automatically stops -- there is no implicit fallthrough to the next case, which is a common point of confusion for people coming from C or Java.
Example: Basic switch
package main
import "fmt"
func main() {
day := "Tue"
switch day {
case "Mon":
fmt.Println("Start of week")
case "Tue", "Wed", "Thu":
fmt.Println("Midweek")
default:
fmt.Println("Other day")
}
}
Login to try C/C++/Java/PHP code in the editor
switch with No Expression
Omitting the expression after switch turns each case into a boolean condition, evaluated in order, functioning like a tidier version of a chain of else-if statements.
Note: This form reads more cleanly than a long if/else-if chain when there are several conditions.
Example: switch with No Expression
package main
import "fmt"
func main() {
score := 82
switch {
case score >= 90:
fmt.Println("A")
case score >= 75:
fmt.Println("B")
default:
fmt.Println("C")
}
}
Login to try C/C++/Java/PHP code in the editor
Using fallthrough
If you do want a case to continue into the next one, Go requires you to say so explicitly with the fallthrough keyword, making the (unusual) behavior visible in the code rather than an easy-to-miss default.
Note: fallthrough is rarely needed -- reach for it only when you deliberately want the next case's code to also run.
Example: Using fallthrough
package main
import "fmt"
func main() {
n := 1
switch n {
case 1:
fmt.Println("one")
fallthrough
case 2:
fmt.Println("two (via fallthrough)")
case 3:
fmt.Println("three")
}
}
Login to try C/C++/Java/PHP code in the editor
switch with an Init Statement
Just like if, a switch can run a short statement before the value it switches on, scoping a variable to the whole switch block -- handy for computing a value once and branching on it.
Example: switch with an Init Statement
package main
import "fmt"
func classify(n int) string {
switch remainder := n % 3; remainder {
case 0:
return "divisible by 3"
default:
return "not divisible by 3"
}
}
func main() {
fmt.Println(classify(9))
fmt.Println(classify(10))
}
Login to try C/C++/Java/PHP code in the editor
- Adding an explicit break at the end of every case out of habit from C/Java -- Go cases don't fall through by default, so it's unnecessary.
- Forgetting that Go's switch can be used with no expression at all, as a cleaner alternative to a long if/else if chain.
- Not knowing about fallthrough and being surprised that omitting it stops the next case from running automatically.
- Go's switch automatically breaks after each case -- no fallthrough unless you ask for it with the fallthrough keyword.
- A switch with no expression works like a cleaner chain of if/else if statements.
- A single case can list multiple comma-separated values.
- switch can include an init statement, just like if.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: