The switch Statement
In this page:
switch expression {
case value1, value2:
// body
case value3:
// body
default:
// body
}
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
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create day and infer the type
day := "Tue"
// Choose a branch based on the value of day
switch day {
case "Mon":
fmt.Println("Start of week")
case "Tue", "Wed", "Thu":
fmt.Println("Midweek")
// default runs when no case matches
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.
Example: switch with No Expression
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create score and infer the type
score := 82
// Choose a branch based on the value
switch {
case score >= 90:
fmt.Println("A")
case score >= 75:
fmt.Println("B")
// default runs when no case matches
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.
Example: Using fallthrough
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create n and infer the type
n := 1
// Choose a branch based on the value of n
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
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// Define the function classify
func classify(n int) string {
// Choose a branch based on the value of remainder := n % 3; remainder
switch remainder := n % 3; remainder {
case 0:
return "divisible by 3"
// default runs when no case matches
default:
return "not divisible by 3"
}
}
// main is where the program starts running
func main() {
// Print the values followed by a newline
fmt.Println(classify(9))
// Print the values followed by a newline
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: