10、switch

118 阅读2分钟

本文为译文。原文地址:switch

由于本文整体概念比较简单,所以译者主要翻译一些go里独有的一些语法点。其余的和其他语言没啥两样。如有需要可以直接看原文。

基本语法

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")
    case "a", "e", "i", "o", "u": //multiple expressions in case
        fmt.Println("vowel")
    default: //default case
        fmt.Println("incorrect finger number")
    }
}

省略表达式

表达式是可以被省略的。如果表达式被省略了就被认为switch true每一个case判断都会被执行。如果对应的case condition equal true则里面的代码会被执行

package main

import (  
    "fmt"
)

func main() {  
    num := 75
    switch { // expression is omitted
    case num >= 0 && num <= 50:
        fmt.Printf("%d is greater than 0 and less than 50", num)
    case num >= 51 && num <= 100:
        fmt.Printf("%d is greater than 51 and less than 100", num)
    case num >= 101:
        fmt.Printf("%d is greater than 100", num)
    }

}

fallthrough

在go中,一个case被执行完成后,程序的control就立刻会从switch的声明中出来。fallthrough被用作转移control到现在case的第一个声明。

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函数初始化。

75 is lesser than 100  
75 is lesser than 200  

fallthrough应该在一个case的最后一句声明。否则会报错fallthrough statement out of place

即使case是false,**fallthrough****也会发生。**个人理解fall 掉下 + through 通过。即直接掉到下面。

package main

import (  
    "fmt"
)

func main() {  
    switch num := 25; { 
    case num < 50:
        fmt.Printf("%d is lesser than 50\n", num)
        fallthrough
    case num > 100:
        fmt.Printf("%d is greater than 100\n", num)     
    }

}

另外一点:**fallthrough**不能用在最后一个case.因为没地方再往下掉了。

breaking switch

break可以用来终止switch,在它完成之前。

下面的程序将不会有任何输出。( the program doesn't print anything)

package main

import (  
    "fmt"
)

func main() {  
    switch num := -5; {
    case num < 50:
        if num < 0 {
            break
        }
        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)
    }

}

跳到for循环外

当switch case在for循环内,那么就可能会提前终止for循环。这里可能就需要label。之前我们有在loop中了解过。

我们现在来写一个无限循环生成数字,当数字为偶数,我们终止循环。

package main

import (  
    "fmt"
    "math/rand"
)

func main() {  
randloop:  
    for {
        //generated between 0 and 99 (100 is not included)
        switch i := rand.Intn(100); { 
        case i%2 == 0:
            fmt.Printf("Generated even number %d", i)
            break randloop
        }
    }

}

注意:如果只使用break,不用label,那么只会终止switch,但是for循环还是持续的。