This is a creation in Article, where the information may have evolved or changed.
33. Toad Notes Go language--switch keywords
The switch of Go is very flexible. The expression does not have to be a constant or an integer, and the process is performed from top to bottom until a match is found, and if switch does not have an expression, it matches true. This creates a possibility--using switch to write if-else-if-else judgment sequences.
It does not match the failure automatically after the attempt is made, but you can use Fallthrough to make it do so.
Use default to specify the behavior when all other branches do not match.
code example:
Package Main
Import "FMT"
Import "Time"
Func Main () {
I: = 2
Fmt. Print ("Write", I, "as")
Switchi {
Case 1:
Fmt. Println ("one")
Case 2:
Fmt. Println ("both")
Case 3:
Fmt. Println ("three")
}
Switchtime. Now (). Weekday () {
Casetime. Saturday, time. Sunday:
Fmt. Println ("It ' sthe Weekend")
Default
Fmt. Println ("It ' sa weekday")
}
T: =time. Now ()
switch{
Caset. Hour () < 12:
Fmt. Println ("It ' sbefore Noon")
Default
Fmt. Println ("It ' safter Noon")
}
}
Output:
Write2 as
It ' sa weekday
It ' safter Noon