Conditional - Switch Statement

Part of Golang Mastery course

~15 min read
Interactive
Hands-on
Beginner-friendly

In this section, we are going to look at the switch statement. This is all part of control flow, which is sequential, iterative (loops), and conditional; control flow is how computers read your program.

By default, it is sequential. You can control the flow of how computers are reading your program with something like an iterative, using the for keyword, you can also use a conditional; an if statement or a switch statement.

A switch statement always starts with the keyword switch, think "Hey, I want to switch on something."

In addition to the switch keyword, a switch statement has cases. The switch statement switches on some case.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
switch {
case false:
fmt.Println(string">"this should not print")
case (2 == 4):
fmt.Println(string">"this should not print2")
case (3 == 3):
fmt.Println(string">"prints")
case (4 == 4):
fmt.Println(string">"also true, does it print?")
}
}
 

playground

In the above, the first case that evaluates to true 3==3 runs the code speficied fmt.Println("prints") and the switch statement exits. There is no fallthrough by default in a switch statement in Go, unless it is specified, which is why the finalcase here 4==4 does not return. Let's specify fallthrough and see what happens.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
switch {
case false:
fmt.Println(string">"this should not print")
case (2 == 4):
fmt.Println(string">"this should not print2")
case (3 == 3):
fmt.Println(string">"prints")
fallthrough
case (4 == 4):
fmt.Println(string">"also true, does it print?")
}
}
 

playground

Because we specified fallthrough, the final case 4==4 also gets run. You can use fallthrough to make each statement evaluate, as in this example.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
switch {
case false:
fmt.Println(string">"this should not print")
case (2 == 4):
fmt.Println(string">"this should not print2")
case (3 == 3):
fmt.Println(string">"prints")
fallthrough
case (4 == 4):
fmt.Println(string">"also true, does it print?")
fallthrough
case (7 == 9):
fmt.Println(string">"not true 1")
fallthrough
case (11 == 14):
fmt.Println(string">"not true 2")
fallthrough
case (15 == 15):
fmt.Println(string">"true 15")
}
}
 

playground

However, generally speaking just don't use fallthrough.

We can also add a default case, which is what happens if nothing else evaluates to true.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
switch {
case false:
fmt.Println(string">"this should not print")
case (2 == 4):
fmt.Println(string">"this should not print2")
default:
fmt.Println(string">"this is default")
}
}
 

playground

If you have a value after the switch statement, it no longer evaluates on a boolean, but rather evaluates on the value.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
switch string">"Bond" {
case string">"Moneypenny":
fmt.Println(string">"miss money")
case string">"Bond":
fmt.Println(string">"bond james")
case string">"Q":
fmt.Println(string">"This is q")
default:
fmt.Println(string">"this is default")
}
}
 

playgound

Rather than hard code the switch statement value, we could use a variable.

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
n := string">"Bond"
switch n {
case string">"Moneypenny":
fmt.Println(string">"miss money")
case string">"Bond":
fmt.Println(string">"bond james")
case string">"Q":
fmt.Println(string">"This is q")
default:
fmt.Println(string">"this is default")
}
}
 

playground

We can also have multiple values for a case statement, as in case "Moneypenny", "Bond", "Dr No"

example.go
package main
 
import (
string">"fmt"
)
 
func main() {
n := string">"Bond"
switch n {
case string">"Moneypenny", string">"Bond", string">"Dr No":
fmt.Println(string">"miss money or bond or dr no")
case string">"M":
fmt.Println(string">"m")
case string">"Q":
fmt.Println(string">"This is q")
default:
fmt.Println(string">"this is default")
}
}
 

playground

Your Progress

23 of 103 modules
22%
Started22% Complete
Previous
SpaceComplete
Next