[关闭]
@phper 2018-03-13T10:14:44.000000Z 字数 3091 阅读 2770

10.switch 语句

Golang

原文:https://golangbot.com/switch


这是 Golang 系列教程中的第 10 章。

switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。它可以被认为是替代多个 if else 子句的常用方式。

看代码比文字更容易理解。让我们从一个简单的例子开始,它将把一个手指的编号作为输入,然后输出该手指对应的名字。比如 0 是拇指,1 是食指等等。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. finger := 4
  7. switch finger {
  8. case 1:
  9. fmt.Println("Thumb")
  10. case 2:
  11. fmt.Println("Index")
  12. case 3:
  13. fmt.Println("Middle")
  14. case 4:
  15. fmt.Println("Ring")
  16. case 5:
  17. fmt.Println("Pinky")
  18. }
  19. }

在线运行程序

在上述程序中,switch fingerfinger 的值与每个 case 语句进行比较。通过从上到下对每一个值进行对比,并执行与选项值匹配的第一个逻辑。在上述样例中, finger 值为 4,因此打印的结果是 Ring

在选项列表中,case 不允许出现重复项。如果您尝试运行下面的程序,编译器会报这样的错误: main.go:18:2:在tmp / sandbox887814166 / main.go:16:7

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. finger := 4
  7. switch finger {
  8. case 1:
  9. fmt.Println("Thumb")
  10. case 2:
  11. fmt.Println("Index")
  12. case 3:
  13. fmt.Println("Middle")
  14. case 4:
  15. fmt.Println("Ring")
  16. case 4://重复项
  17. fmt.Println("Another Ring")
  18. case 5:
  19. fmt.Println("Pinky")
  20. }
  21. }

在线运行程序

默认情况(Default Case)

我们每个人一只手只有 5 个手指。如果我们输入了不正确的手指编号会发生什么?这个时候就应该是属于默认情况。当其他情况都不匹配时,将运行默认情况。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. switch finger := 8; finger {
  7. case 1:
  8. fmt.Println("Thumb")
  9. case 2:
  10. fmt.Println("Index")
  11. case 3:
  12. fmt.Println("Middle")
  13. case 4:
  14. fmt.Println("Ring")
  15. case 5:
  16. fmt.Println("Pinky")
  17. default: // 默认情况
  18. fmt.Println("incorrect finger number")
  19. }
  20. }

在线运行程序

在上述程序中 finger 的值是 8,它不符合其中任何情况,因此会打印 incorrect finger number。default 不一定只能出现在 switch 语句的最后,它可以放在 switch 语句的任何地方。

您可能也注意到我们稍微改变了 finger 变量的声明方式。finger 声明在了 switch 语句内。在表达式求值之前,switch 可以选择先执行一个语句。在这行 switch finger:= 8; finger 中, 先声明了finger 变量,随即在表达式中使用了它。在这里,finger 变量的作用域仅限于这个 switch 内。

多表达式判断

通过用逗号分隔,可以在一个 case 中包含多个表达式。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. letter := "i"
  7. switch letter {
  8. case "a", "e", "i", "o", "u": // 一个选项多个表达式
  9. fmt.Println("vowel")
  10. default:
  11. fmt.Println("not a vowel")
  12. }
  13. }

在线运行程序

case "a","e","i","o","u": 这一行中,列举了所有的元音。只要匹配该项,则将输出 vowel

无表达式的 switch

在 switch 语句中,表达式是可选的,可以被省略。如果省略表达式,则表示这个 switch 语句等同于 switch true,并且每个 case 表达式都被认定为有效,相应的代码块也会被执行。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. num := 75
  7. switch { // 表达式被省略了
  8. case num >= 0 && num <= 50:
  9. fmt.Println("num is greater than 0 and less than 50")
  10. case num >= 51 && num <= 100:
  11. fmt.Println("num is greater than 51 and less than 100")
  12. case num >= 101:
  13. fmt.Println("num is greater than 100")
  14. }
  15. }

在线运行程序

在上述代码中,switch 中缺少表达式,因此默认它为 true,true 值会和每一个 case 的求值结果进行匹配。case num >= 51 && <= 100: 为 true,所以程序输出 num is greater than 51 and less than 100。这种类型的 switch 语句可以替代多个 if else 子句。

Fallthrough 语句

在 Go 中,每执行完一个 case 后,会从 switch 语句中跳出来,不再做后续 case 的判断和执行。使用 fallthrough 语句可以在已经执行完成的 case 之后,把控制权转移到下一个 case 的执行代码中。

让我们写一个程序来理解 fallthrough。我们的程序将检查输入的数字是否小于 50、100 或 200。例如我们输入 75,程序将输出75 is lesser than 10075 is lesser than 200。我们用 fallthrough 来实现了这个功能。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func number() int {
  6. num := 15 * 5
  7. return num
  8. }
  9. func main() {
  10. switch num := number(); { // num is not a constant
  11. case num < 50:
  12. fmt.Printf("%d is lesser than 50\n", num)
  13. fallthrough
  14. case num < 100:
  15. fmt.Printf("%d is lesser than 100\n", num)
  16. fallthrough
  17. case num < 200:
  18. fmt.Printf("%d is lesser than 200", num)
  19. }
  20. }

在线运行程序

switch 和 case 的表达式不一定是常量。它们也可以在运行过程中通过计算得到。在上面的程序中,num 被初始化为函数 number() 的返回值。程序运行到 switch 中时,会计算出 case 的值。case num < 100: 的结果为 true,所以程序输出 75 is lesser than 100。当执行到下一句 fallthrough 时,程序控制直接跳转到下一个 case 的第一个执行逻辑中,所以打印出 75 is lesser than 200。最后这个程序的输出会是

  1. 75 is lesser than 100
  2. 75 is lesser than 200

fallthrough 语句应该是 case 子句的最后一个语句。如果它出现在了 case 语句的中间,编译器将会报错:fallthrough statement out of place

这也是我们本教程的最后内容。还有一种 switch 类型称为 type switch 。我们会在学习接口的时候再研究这个。

希望您能享受本次阅读。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注