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.
package mainimport (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?")}}
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 mainimport (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")fallthroughcase (4 == 4):fmt.Println(string">"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 mainimport (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")fallthroughcase (4 == 4):fmt.Println(string">"also true, does it print?")fallthroughcase (7 == 9):fmt.Println(string">"not true 1")fallthroughcase (11 == 14):fmt.Println(string">"not true 2")fallthroughcase (15 == 15):fmt.Println(string">"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 mainimport (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")}}
If you have a value after the switch statement, it no longer evaluates on a boolean, but rather evaluates on the value.
package mainimport (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")}}
Rather than hard code the switch statement value, we could use a variable.
package mainimport (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")}}
We can also have multiple values for a case statement, as in case "Moneypenny", "Bond", "Dr No"
package mainimport (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")}}