該教程是Golang系列教程的第10個
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")
}
}
在上面的程序中,finger
是 8
,它與任何 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)
}
Switch
和 case
表達式需要的不僅僅只是常量。他們也可以在運行時被評估。上面程序中,num
被 number()
函數的返回值初始化。控制權進入 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
我們的教程就要結束了,還有一種類型的 switch
是 type switch。我們將在學習接口的時候遇到。
下一教程 - 數組和切片