1. 程式人生 > 其它 >Go switch語句

Go switch語句

10. switch 語句

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

看程式碼比文字更容易理解。讓我們從一個簡單的例子開始,它將把一個手指的編號作為輸入,然後輸出該手指對應的名字。比如 0 是拇指,1 是食指等等。

Copy
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:7

Copy
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 個手指。如果我們輸入了不正確的手指編號會發生什麼?這個時候就應該是屬於預設情況。當其他情況都不匹配時,將執行預設情況。

Copy
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 內。

多表達式判斷

通過用逗號分隔,可以在一個 case 中包含多個表示式。

Copy
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": 這一行中,列舉了所有的母音。只要匹配該項,則將輸出 vowel

無表示式的 switch

在 switch 語句中,表示式是可選的,可以被省略。如果省略表示式,則表示這個 switch 語句等同於 switch true,並且每個 case 表示式都被認定為有效,相應的程式碼塊也會被執行。

Copy
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 >= 51 && <= 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 來實現了這個功能。

Copy
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。當執行到下一句 fallthrough 時,程式控制直接跳轉到下一個 case 的第一個執行邏輯中,所以打印出 75 is lesser than 200。最後這個程式的輸出會是

Copy
75 is lesser than 100  
75 is lesser than 200

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

switch語句

package main

import "fmt"

//switch

func main() {

   // **********基本使用
   //var a int =10
   //switch a {
   //case 1:
   // fmt.Println("是1")
   //case 2:
   // fmt.Println("是2")
   //case 10:
   // fmt.Println("是10")
   //}


   //***********default
   //var a int =2
   //switch a {
   //case 1:
   // fmt.Println("是1")
   //case 2:
   // fmt.Println("是2")
   //case 10:
   // fmt.Println("是10")
   //default:
   // //上面都沒匹配,執行這句
   // fmt.Println("我不知道是幾")
   //}

   //**********多表達式
   //var a int =9
   //switch a {
   //case 1,3,5,7,9:
   // fmt.Println("是1")
   //case 2,4,6,8:
   // fmt.Println("是2")
   //case 10,30,60:
   // fmt.Println("是10")
   //default:
   // //上面都沒匹配,執行這句
   // fmt.Println("我不知道是幾")
   //}

   //*******無表示式
   //var a int =2
   //switch  {
   //case a==2:
   // fmt.Println("22")
   //case a==3:
   // fmt.Println("33")
   //default:
   // fmt.Println("b到")
   //
   //}

   //*******Fallthrough
   var a int =2
   switch  {
   case a==2:
      fmt.Println("22")
      fallthrough  //穿透,無條件執行下一個case
   case a==3:
      fmt.Println("33")
      fallthrough
   default:
      fmt.Println("b到")
   }

}
View Code