Go switch語句

10. switch 語句

switch 是一個條件語句,用於將表達式的值與可能匹配的選項列表進行比較,並根據匹配狀況執行相應的代碼塊。它能夠被認爲是替代多個 if else 子句的經常使用方式。less

看代碼比文字更容易理解。讓咱們從一個簡單的例子開始,它將把一個手指的編號做爲輸入,而後輸出該手指對應的名字。好比 0 是拇指,1 是食指等等。ide

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 fingerfinger 的值與每一個 case 語句進行比較。經過從上到下對每個值進行對比,並執行與選項值匹配的第一個邏輯。在上述樣例中, finger 值爲 4,所以打印的結果是 Ring函數

在選項列表中,case 不容許出現重複項。若是您嘗試運行下面的程序,編譯器會報這樣的錯誤: main.go:18:2:在tmp / sandbox887814166 / main.go:16:7code

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://重複項
        fmt.Println("Another Ring")
    case 5:
        fmt.Println("Pinky")

    }
}

默認狀況(Default Case)

咱們每一個人一隻手只有 5 個手指。若是咱們輸入了不正確的手指編號會發生什麼?這個時候就應該是屬於默認狀況。當其餘狀況都不匹配時,將運行默認狀況。作用域

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: // 默認狀況
        fmt.Println("incorrect finger number")
    }
}

在上述程序中 finger 的值是 8,它不符合其中任何狀況,所以會打印 incorrect finger number。default 不必定只能出如今 switch 語句的最後,它能夠放在 switch 語句的任何地方。編譯器

您可能也注意到咱們稍微改變了 finger 變量的聲明方式。finger 聲明在了 switch 語句內。在表達式求值以前,switch 能夠選擇先執行一個語句。在這行 switch finger:= 8; finger 中, 先聲明瞭finger 變量,隨即在表達式中使用了它。在這裏,finger 變量的做用域僅限於這個 switch 內。it

多表達式判斷

經過用逗號分隔,能夠在一個 case 中包含多個表達式。編譯

package main

import (
    "fmt"
)

func main() {
    letter := "i"
    switch letter {
    case "a", "e", "i", "o", "u": // 一個選項多個表達式
        fmt.Println("vowel")
    default:
        fmt.Println("not a vowel")
    }
}

case "a","e","i","o","u": 這一行中,列舉了全部的元音。只要匹配該項,則將輸出 vowelclass

無表達式的 switch

在 switch 語句中,表達式是可選的,能夠被省略。若是省略表達式,則表示這個 switch 語句等同於 switch true,而且每一個 case 表達式都被認定爲有效,相應的代碼塊也會被執行。import

package main

import (
    "fmt"
)

func main() {
    num := 75
    switch { // 表達式被省略了
    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 中缺乏表達式,所以默認它爲 true,true 值會和每個 case 的求值結果進行匹配。case num &gt;= 51 && &lt;= 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 來實現了這個功能。

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 &lt; 100: 的結果爲 true,因此程序輸出 75 is lesser than 100。當執行到下一句 fallthrough 時,程序控制直接跳轉到下一個 case 的第一個執行邏輯中,因此打印出 75 is lesser than 200。最後這個程序的輸出會是

75 is lesser than 100  
75 is lesser than 200

fallthrough 語句應該是 case 子句的最後一個語句。若是它出如今了 case 語句的中間,編譯器將會報錯:fallthrough statement out of place

相關文章
相關標籤/搜索