简析go语言中select和switch
select类似于switch语句,但是只能用于channel操作,可以用于channel的数据接收也可以用于channel的数据发送。每个case必须是channel操作(接收或者发送数据)。select分支执行顺序如下:
如果select多个case的channel都准备好了,select随机选择一个case执行,其他case不会执行。
func main() { c1 := make(chan int) c2 := make(chan int) c3 := make(chan int) go func() { c1 <- 1 }() go func() { c2 <- 1 }() go func() { c3 <- 1 }() select { case msg1 := <-c1: if msg1 == 1 { print("This is case msg1", "\n") } case msg2 := <-c2: if msg2 == 1 { print("This is case msg2", "\n") } case msg3 := <-c3: if msg3 == 1 { print("This is case msg3", "\n") } } } $ go run select_example.go This is case msg1
如果select多个case任意channel准备好了,select选择先准备好的case执行,其他的case不会执行。
import "time" func main() { c1 := make(chan int) c2 := make(chan int) c3 := make(chan int) go func() { time.Sleep(time.Second * 5) c1 <- 1 }() go func() { time.Sleep(time.Second * 5) c2 <- 1 }() go func() { time.Sleep(time.Second * 1) c3 <- 1 }() select { case msg1 := <-c1: if msg1 == 1 { print("This is case msg1", "\n") } case msg2 := <-c2: if msg2 == 1 { print("This is case msg2", "\n") } case msg3 := <-c3: if msg3 == 1 { print("This is case msg3", "\n") } } } $ go run select_example.go This is case msg3
如果select多个case都没有channel准备好且有default分支,select选择default执行,其他的case不会执行。
import "time" func main() { c1 := make(chan int) c2 := make(chan int) c3 := make(chan int) go func() { time.Sleep(time.Second * 5) c1 <- 1 }() go func() { time.Sleep(time.Second * 5) c2 <- 1 }() go func() { time.Sleep(time.Second * 1) c3 <- 1 }() select { case msg1 := <-c1: if msg1 == 1 { print("This is case msg1", "\n") } case msg2 := <-c2: if msg2 == 1 { print("This is case msg2", "\n") } case msg3 := <-c3: if msg3 == 1 { print("This is case msg3", "\n") } default: print("There are no channel ready, so exit!") } } $ go run select_example.go There are no channel ready, so exit!
如果select多个case都没有channel准备好且没有default分支,select会阻塞,直到有channel准备好。
import "time" func main() { c1 := make(chan int) c2 := make(chan int) c3 := make(chan int) go func() { time.Sleep(time.Second * 1) c1 <- 1 }() go func() { time.Sleep(time.Second * 1) c2 <- 1 }() go func() { time.Sleep(time.Second * 1) c3 <- 1 }() select { case msg1 := <-c1: if msg1 == 1 { print("This is case msg1", "\n") } case msg2 := <-c2: if msg2 == 1 { print("This is case msg2", "\n") } case msg3 := <-c3: if msg3 == 1 { print("This is case msg3", "\n") } } } $ go run select_example.go This is case msg3
switch可以为各种类型进行分支操作,各个case分支是顺序求值的,且可以为接口类型进行分支判断(i.(type),即类型断言)。
import "fmt" func printArgs(args ...interface{}) { // ...interface{}在函数体内部呈现为[]interface{} for _, v := range args { switch v.(type) { case int: fmt.Println("receive int args", v) case string: fmt.Println("receive string args", v) default: fmt.Println("receive other types but not int or string") } } } func main() { printArgs('a', "b", 1, 2, 3) println("-----------------") //s := []int{1, 2, 3, 4, 5} //printArgs(s...) 此处会编译报错:panic: interface conversion: interface {} is string, not int32。这是需要注意的一个点,虽然空接口类型变量可以赋予任意类型的值,但是任意类型的切片并不能赋值给空接口类型的切片 s := []interface{}{1, 2, 3, 4, 5} printArgs(s...) } $ go run switch_example.go receive other types but not int or string receive string args b receive int args 1 receive int args 2 receive int args 3 ----------------- receive int args 1 receive int args 2 receive int args 3 receive int args 4 receive int args 5
关于类似于function(args ...T)中的...T写法,参见往期分析:https://www.nowcoder.com/issue/tutorial?zhuanlanId=0k81bm&uuid=070b83d4ce9a4c70be72931823bd4c01
关于select和switch中表达式的求值顺序,参见往期分析:https://www.nowcoder.com/issue/tutorial?zhuanlanId=0k81bm&uuid=cdc5bbfc7845458abc33519663ba72a2
Go语言学习笔记、语法知识、技术要点和个人理解及实战