The main reason for presenting the switch statement in this chapter is because a switch case can use regular expressions. Firstly, however, take a look at this simple switch block:
switch asString {
case "1":
fmt.Println("One!")
case "0":
fmt.Println("Zero!")
default:
fmt.Println("Do not care!")
}
The preceding switch block can differentiate between the "1" string, the "0" string, and everything else (default).
Having a match all remaining cases case in a switch block is considered a very good practice. However, as the order of the cases in a switch block does matter, the match all remaining cases case should be put last. In Go, the name of the match all remaining cases case is default.
However, a switch statement can be much more flexible...