Part10: Switch语句

该教程是Golang系列教程的第10个

文章目录

  • Default 分支
  • case 的多个表达式
  • switch 表达式
  • Fallthrough

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 从上到下进行评估,并执行与表达式匹配的第一个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")

    }
}

Default 分支

我们的手上只有 5 个手指。如果我们输出一个错误的的手指数,会发生什么。这就是 default 分支使用的地方。当没有 case 被匹配,将执行 default。

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")
    }
}

在上面的程序中,finger8,它与任何 case 都不匹配,因此 incorrect finger number 被打印。如果不是必要的话,default 应该是 switch 语句的最后一个。它可以出现在 switch 的任何地方。

你有一个小地方需要注意 finger 声明的修改。它声明在 switch 本身里。switch 包含一个可选的语句,它在表达式被计算之前执行。在这一行 switch finger := 8; finger finger 首先被声明,也在表达式中被使用。这个 finger 的作用域被限制在 swith 块中。

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")
    }
}

上面的程序检查 letter 是否是元音。case "a", "e", "i", "o", "u":匹配所有的元音。程序打印 vowel

switch 表达式

switch 中的表达式是可选的,它们可以被忽略。如果表达式被忽略,switch 被认为是 switch true,对每个case 表达式进行真值评估,并执行相应的代码块。

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")
    }

}

在上面的程序中,表达式不存在,因此它被认为是 true,每个 case 被评估。case num >= 51 && num <= 100:true,程序输出 num is greater than 51 ang less than 100。这种类型的 switch 被认为可以和多个 if else 子句替换。

Fallthrough

在Go中,控制在执行 case 后立即从 switch 语句中出来。fallthouth 语句用于将控制转移到在已执行的 case 之后立即出现的 case 的第一个语句。

我们用一个程序来理解 fallthough。我们的程序会检查输入的数字是否小于50,100,200。例如如果我们输入 75, 程序会打印 75 小于 100 和 200。我们使用 fallthough 获得这个输出

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)
    }

Switchcase 表达式需要的不仅仅只是常量。他们也可以在运行时被评估。上面程序中,numnumber() 函数的返回值初始化。控制权进入 switch 内部,case 被评估。case num < 100: 是 true,程序打印 75 is lesser than 100。下一个语句是 fallthough。当遇到 fallthough,控制权被转换到下一个 case 的第一个语句,打印 75 is lesser than 200。程序的输出是

75 is lesser than 100  
75 is lesser than 200  

fallthough 应该是一个 case 的最后一个语句,如果它出现在中间的某个地方,编译器将抛出错误 fallthough statement out of place

我们的教程就要结束了,还有一种类型的 switchtype switch。我们将在学习接口的时候遇到。

下一教程 - 数组和切片

你可能感兴趣的:([译]Golang教程系统)