switch 是一個條件語句,用於將一個表達式的求值結果與可能的值的列表進行匹配,並根據匹配結果執行相應的代碼。可以認為 switch 語句是編寫多個 if-else 子句的替代方式。
舉例是說明問題最好的方式,讓我們寫一個簡單的程序,輸入手指編號,輸出對應的手指名稱:)。例如 0 表示拇指,1 表示食指等。
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") } }
在上面的程序中,case 語句依次(從上到下)求值並與 finger 進行匹配,直到找到第一個與 finger 匹配的 case,並執行其中的代碼。在這里 finger 的值為 4,因此打印 Ring。
多個有相同值的 case 是不允許的。如果你運行下面的程序,編譯將會報錯:duplicate case 4 in switch。
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 case
我們每只手只有 5 根手指,但是如果我們輸入一個錯誤的手指序號會發生什么呢?這里就要用到 default 語句了。當沒有其他 case 匹配時,將執行 default 語句。
package main import ( "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 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 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 后面沒有表達式因此被認為是 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 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 中的表達式求值並與 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,我們將在學習接口時介紹它。
本文摘自:https://blog.csdn.net/u011304970/article/details/74936112
