Similar to if, there can also be an initialization statement after the switch statement.
For example:
switch num1:=2; num1{
case 1:
println("1")
case 2:
println("2")
case 3:
println("2")
}
The following example shows a slightly different usage from the above.
The following is a complete code example.
package main
import "fmt"
func main() {
switch hour := 8; {
case hour < 12:
fmt.Println("Good morning!")
case hour < 17:
fmt.Println("Good afternoon.")
default:
fmt.Println("Good evening.")
}
}
Below is the program output:
Good morning!