- 原文地址:Part 10: Switch Statement
- 原文作者:Naveen R
- 译者:咔叽咔叽 转载请注明出处。
switch是一个条件语句,它计算表达式并将其和可能的结果进行匹配,根据匹配执行代码块。它常用来替代多个 if else 子句。
光说不练假把式,让我们从一个简单的例子开始,该例子将手指号作为输入并输出该手指的名称:)。例如 1 是拇指,2 是无名指,依此类推。
package main
import (
"fmt"
)
func main() {
finger := 4
switch finger {
case 1:
fmt.Println("Thumb")
case 2:
fmt.Println("Index")
case 3:
fmt.Println("Middle")
case 4:
fmt.Println("Ring")
case 5:
fmt.Println("Pinky")
}
}
在上述程序中,switch finger将finger的值与每个 case 语句进行比较。该语句从上到下进行比较,并执行与表达式匹配的第一个case。在这个例子中,finger的值为 4,因此打印Ring。
具有相同值的case是不被允许的。如果你试图运行下面的程序,编译器会报错main.go:18:2: duplicate case 4 in switch previous case at tmp/sandbox887814166/main.go:16:7
package main
import (
"fmt"
)
func main() {
finger := 4
switch finger {
case 1:
fmt.Println("Thumb")
case 2:
fmt.Println("Index")
case 3:
fmt.Println("Middle")
case 4:
fmt.Println("Ring")
case 4://duplicate case
fmt.Println("Another Ring")
case 5:
fmt.Println("Pinky")
}
}
默认的 case
我们手中只有 5 个手指。输入错误的手指号会发生什么?这是 默认的 case的用武之地。当其他case都不匹配时,将执行默认case。
package main
import (
"fmt"
)
func main() {
switch finger := 8; finger {
case 1:
fmt.Println("Thumb")
case 2:
fmt.Println("Index")
case 3:
fmt.Println("Middle")
case 4:
fmt.Println("Ring")
case 5:
fmt.Println("Pinky")
default: //default case
fmt.Println("incorrect finger number")
}
}
在上面的程序中,finger的值是 8 并且它与任何case都不匹配,因此打印incorrect finger number。默认case不一定是 switch 语句中的最后一个。它可以出现在switch的任何位置。
你可能注意到声明finger的一个小变化,它在switch中声明。switch可以包含一个可选的语句,它在表达式计算之前执行。在该行,switch finger := 8; finger,首先声明了finger并在表达式中使用,而finger的范围仅限于switch代码块。
case 可以拥有多个表达式
可以在一个case中用逗号分隔实现多个表达式。
package main
import (
"fmt"
)
func main() {
letter := "i"
switch letter {
case "a", "e", "i", "o", "u": //multiple expressions in case
fmt.Println("vowel")
default:
fmt.Println("not a vowel")
}
}
上面的程序检查字母是否是元音。case "a", "e", "i", "o", "u":匹配所有元音。由于letter的值为i该程序输出vowel
表达式 switch
switch中的表达式是可选的,可以省略。如果省略表达式,则switch true,每个case表达式都视为true,并且执行相应的代码块。
package main
import (
"fmt"
)
func main() {
num := 75
switch { // expression is omitted
case num >= 0 && num <= 50:
fmt.Println("num is greater than 0 and less than 50")
case num >= 51 && num <= 100:
fmt.Println("num is greater than 51 and less than 100")
case num >= 101:
fmt.Println("num is greater than 100")
}
}
在上述程序中,switch没有表达式,所以它被认为是true,并且会执行每一个case。case num >= 51 && num <= 100:为true且程序输出num is greater than 51 and less than 100。这种类型的switch可以用来替代多个if else的情况。
fallthrough
在 Go 语言中,在执行case后立即从switch语句中返回。 fallthrough语句用于执行该case之后,再执行下一个case语句。
package main
import (
"fmt"
)
func number() int {
num := 15 * 5
return num
}
func main() {
switch num := number(); { //num is not a constant
case num < 50:
fmt.Printf("%d is lesser than 50\n", num)
fallthrough
case num < 100:
fmt.Printf("%d is lesser than 100\n", num)
fallthrough
case num < 200:
fmt.Printf("%d is lesser than 200", num)
}
}
switch和case表达式不一定只能是常量。它们也可以在运行时进行赋值。在上面的程序中,num被初始化为函数number()的返回值。然后进入case语句的计算,case num < 100:为true则程序打印75 is lesser than 100,下一个语句是fallthrough。当遇到fallthrough时,将会执行下一个case的语句,并且打印75 is lesser than 200,程序的输出是
75 is lesser than 100
75 is lesser than 200
fallthrough应该位于case语句中的最后。如果它出现在中间的某个位置,编译器会抛出错误fallthrough statement out of place