Use the switch
statement to select one of many code blocks to be executed.
The switch
statement in Go is similar to the ones in C, C++, Java, JavaScript, and PHP. The difference is that it only runs the matched case so it does not need a break
statement.
switch
expression {
case
x:
// code block
case
y:
// code block
case
z:
...
default:
// code block
}
This is how it works:
switch
expression is compared with the values of each case
default
keyword is optional. It specifies some code to run if there is no case
matchThe example below uses a weekday number to calculate the weekday name:
package main
import ("fmt")
func main() {
day := 4
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
case 7:
fmt.Println("Sunday")
}
}
Result:
Thursday
The default
keyword specifies some code to run if there is no case match:
package main
import ("fmt")
func main() {
day := 8
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
case 4:
fmt.Println("Thursday")
case 5:
fmt.Println("Friday")
case 6:
fmt.Println("Saturday")
case 7:
fmt.Println("Sunday")
default:
fmt.Println("Not a weekday")
}
}
Result:
Not a weekday
All the case
values should have the same type as the switch
expression. Otherwise, the compiler will raise an error:
package main
import ("fmt")
func main() {
a := 3
switch a {
case 1:
fmt.Println("a is one")
case "b":
fmt.Println("a is b")
}
}
Result:
./prog.go:11:2: cannot use "b" (type untyped string) as type int
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!