這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
原文:https://golangbot.com/switch/
這是本Golang系列教程的第十篇。
switch 是一個條件陳述式,用於將一個運算式的求值結果與可能的值的列表進行匹配,並根據匹配結果執行相應的代碼。可以認為 switch 語句是編寫多個 if-else 子句的替代方式。
舉例是說明問題最好的方式,讓我們寫一個簡單的程式,輸入手指編號,輸出對應的手指名稱:)。例如 0 表示拇指,1 表示食指等。
package mainimport ( "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") }}
在上面的程式中,case 語句依次(從上到下)求值並與 finger 進行匹配,直到找到第一個與 finger 匹配的 case,並執行其中的代碼。在這裡 finger 的值為 4,因此列印 Ring。
多個有相同值的 case 是不允許的。如果你運行下面的程式,編譯將會報錯:duplicate case 4 in switch。
package mainimport ( "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 case
我們每隻手只有 5 根手指,但是如果我們輸入一個錯誤的手指序號會發生什麼呢?這裡就要用到 default 語句了。當沒有其他 case 匹配時,將執行 default 語句。
package mainimport ( "fmt")func main() { switch finger := 8; finger {//finger is declared in switch 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 首先被聲明,然後作為運算式被求值。這種方式(譯者註:在 switch 語句中聲明變數的方式)聲明的 finger 只能在 switch 語句中訪問。
包含多個運算式的 case
可以在一個 case 中包含多個運算式,每個運算式用逗號分隔。
package mainimport ( "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 true,這種情況下會將每一個 case 的運算式的求值結果與 true 做比較,如果相等,則執行相應的代碼。
package mainimport ( "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 後面沒有運算式因此被認為是 switch true 並對每一個 case 運算式的求值結果與 true 做比較。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 語句。
讓我們寫一個程式來瞭解 fallthrough。下面的程式檢測 number 是否小於 50,100 或 200。例如,如果我們輸入75,程式將列印 75 小於 100 和 200,這是通過 fallthrough 語句實現的。
package mainimport ( "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 中的運算式求值並與 true 做匹配。匹配到 case num < 100: 時結果是 true,因此程式列印:75 is lesser than 100,接著程式遇到 fallthrough 語句,因此繼續對下一個 case 中的運算式求值並與 true 做匹配,結果仍然是 true,因此列印:75 is lesser than 200。最後的輸出如下:
75 is lesser than 100 75 is lesser than 200
fallthrough 必須是 case 語句塊中的最後一條語句。如果它出現在語句塊的中間,編譯器將會報錯:fallthrough statement out of place。
還有一種 switch 語句叫做 type switch,我們將在學習介面時介紹它。
switch 語句的介紹到此結束。感謝閱讀。
目錄
上一篇:Golang教程:(九)迴圈語句
下一篇:Golang教程:(十一)數組和切片