最近一直在寫go, switch說實話用的不算多。但是今天用了下發現go的switch可真不太一樣啊。
無需break
func main() {
i := 0
switch i {
case 0:
fmt.Println("0000000000")
fmt.Println("0")
case 1:
fmt.Println("1111111111")
fmt.Println("1")
case 2:
fmt.Println("2222222222")
fmt.Println("2")
default:
fmt.Println("3333333")
}
該代碼只會匹配到 case 0 ,go會幫你隱式break掉。
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 首先被聲明,然后作為表達式被求值。這種方式聲明的 finger 只能在 switch 語句中訪問。
switch語句對case表達式的結果類型有如下要求:
要求case表達式的結果能轉換為switch表示式結果的類型
並且如果switch或case表達式的是無類型的常量時,會被自動轉換為此種常量的默認類型的值。比如整數1的默認類型是int, 浮點數3.14的默認類型是float64
func main() {
func main() {
value1 := [...]int8{0, 1, 2, 3, 4, 5, 6}
switch 1 + 3 {
case value1[0], value1[1]:
fmt.Println("0 or 1")
case value1[2], value1[3]:
fmt.Println("2 or 3")
case value1[4], value1[5], value1[6]:
fmt.Println("4 or 5 or 6")
}
}
}
switch 表達式的結果是int類型,case表達式的結果是int8類型,而int8不能轉換為int類型,所以上述會報錯誤
./main.go:10:1: invalid case value1[0] in switch on 1 + 3 (mismatched types int8 and int)
包含多個表達式的 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 語句實現的。
這里要注意:fallthrough強制執行后面的case代碼,fallthrough不會判斷下一條case的expr結果是否為true。
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。
Type Switch 的基本用法
Type Switch 是 Go 語言中一種特殊的 switch 語句,它比較的是類型而不是具體的值。它判斷某個接口變量的類型,然后根據具體類型再做相應處理。注意,在 Type Switch 語句的 case 子句中不能使用fallthrough。
它的用法如下。
switch x.(type) {
case Type1:
doSomeThingWithType1()
case Type2:
doSomeThingWithType2()
default:
doSomeDefaultThing()
}
其中,x必須是一個接口類型的變量,而所有的case語句后面跟的類型必須實現了x的接口類型。
為了便於理解,我們可以結合下面這個例子來看:
package main
import (
"fmt"
"reflect"
)
type Animal interface {
shout() string
}
type Dog struct {}
func (self Dog) shout() string {
return fmt.Sprintf("wang wang")
}
type Cat struct {}
func (self Cat) shout() string {
return fmt.Sprintf("miao miao")
}
func main() {
var animal Animal = Dog{}
switch animal.(type) {
case Dog:
fmt.Println("animal'type is Dog")
case Cat:
fmt.Println("animal'type is Cat")
}
}
在上面的例子中,Cat和Dog類型都實現了接口Animal,所以它們可以跟在case語句后面,判斷接口變量animal是否是對應的類型。
在Switch的語句表達式中聲明變量
如果我們不僅想要判斷某個接口變量的類型,還想要獲得其類型轉換后的值的話,我們可以在 Switch 的語句表達式中聲明一個變量來獲得這個值。
其用法如下所示:
package main
import (
"fmt"
"reflect"
)
type Animal interface {
shout() string
}
type Dog struct {
name string
}
func (self Dog) shout() string {
return fmt.Sprintf("wang wang")
}
type Cat struct {
name string
}
func (self Cat) shout() string {
return fmt.Sprintf("miao miao")
}
type Tiger struct {
name string
}
func (self Tiger) shout() string {
return fmt.Sprintf("hou hou")
}
func main() {
// var animal Animal = Tiger{}
// var animal Animal // 驗證 case nil
// var animal Animal = Wolf{} // 驗證 default
var animal Animal = Dog{}
switch a := animal.(type) {
case nil: // a的類型是 Animal
fmt.Println("nil", a)
case Dog, Cat: // a的類型是 Animal
fmt.Println(a) // 輸出 {}
// fmt.Println(a.name) 這里會報錯,因為 Animal 類型沒有成員name
case Tiger: // a的類型是 Tiger
fmt.Println(a.shout(), a.name) // 這里可以直接取出 name 成員
default: // a的類型是 Animal
fmt.Println("default", reflect.TypeOf(a), a)
}
}
在上述代碼中,我們可以看到a := animal.(type)語句隱式地為每個case子句聲明了一個變量a。
變量a類型的判定規則如下:
- 如果
case后面跟着一個類型,那么變量a在這個case子句中就是這個類型。例如在case Tiger子句中a的類型就是Tiger - 如果
case后面跟着多個類型,那么變量a的類型就是接口變量animal的類型,例如在case Dog, Cat子句中a的類型就是Animal - 如果
case后面跟着nil,那么變量a的類型就是接口變量animal的類型Animal,通常這種子句用來判斷未賦值的接口變量 default子句中變量a的類型是接口變量animal的類型
為了更好地理解上述規則,我們可以用if語句和類型斷言來重寫這個switch語句,如下所示:
v := animal // animal 只會被求值一次
if v == nil { // case nil 子句
a := v
fmt.Println("nil", a)
} else if a, isTiger := v.(Tiger); isTiger { // case Tiger 子句
fmt.Println(a.shout(), a.name)
} else {
_, isDog := v.(Dog)
_, isCat := v.(Cat)
if isDog || isCat { // case Dog, Cat 子句
a := v
fmt.Println(a)
// fmt.Println(a.name)
} else { // default 子句
a := v
fmt.Println("default", reflect.TypeOf(a), a)
}
}
