21.Conditional - Switch Statement - Programming in GO
Conditional - Switch Statement
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 case
s. The switch
statement switches on some case
.
package main
import (
"fmt"
)
func main() {
switch {
case false:
fmt.Println("this should not print")
case (2 == 4):
fmt.Println("this should not print2")
case (3 == 3):
fmt.Println("prints")
case (4 == 4):
fmt.Println("also true, does it print?")
}
}
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.
package main
import (
"fmt"
)
func main() {
switch {
case false:
fmt.Println("this should not print")
case (2 == 4):
fmt.Println("this should not print2")
case (3 == 3):
fmt.Println("prints")
fallthrough
case (4 == 4):
fmt.Println("also true, does it print?")
}
}
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.
package main
import (
"fmt"
)
func main() {
switch {
case false:
fmt.Println("this should not print")
case (2 == 4):
fmt.Println("this should not print2")
case (3 == 3):
fmt.Println("prints")
fallthrough
case (4 == 4):
fmt.Println("also true, does it print?")
fallthrough
case (7 == 9):
fmt.Println("not true 1")
fallthrough
case (11 == 14):
fmt.Println("not true 2")
fallthrough
case (15 == 15):
fmt.Println("true 15")
}
}
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.
package main
import (
"fmt"
)
func main() {
switch {
case false:
fmt.Println("this should not print")
case (2 == 4):
fmt.Println("this should not print2")
default:
fmt.Println("this is default")
}
}
If you have a value after the switch
statement, it no longer evaluates on a boolean, but rather evaluates on the value.
package main
import (
"fmt"
)
func main() {
switch "Bond" {
case "Moneypenny":
fmt.Println("miss money")
case "Bond":
fmt.Println("bond james")
case "Q":
fmt.Println("This is q")
default:
fmt.Println("this is default")
}
}
Rather than hard code the switch
statement value, we could use a variable.
package main
import (
"fmt"
)
func main() {
n := "Bond"
switch n {
case "Moneypenny":
fmt.Println("miss money")
case "Bond":
fmt.Println("bond james")
case "Q":
fmt.Println("This is q")
default:
fmt.Println("this is default")
}
}
We can also have multiple values for a case
statement, as in case "Moneypenny", "Bond", "Dr No"
package main
import (
"fmt"
)
func main() {
n := "Bond"
switch n {
case "Moneypenny", "Bond", "Dr No":
fmt.Println("miss money or bond or dr no")
case "M":
fmt.Println("m")
case "Q":
fmt.Println("This is q")
default:
fmt.Println("this is default")
}
}