golang中select和switch的区别

本文介绍了Go语言中select和switch的区别与应用场景。select专用于channel的读写操作,而switch则适用于各种类型的条件判断,包括对接口类型的检查。文章通过示例代码展示了这两种结构的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select 和 switch 是 Go语言中进行分支操作的两个方式,各有各的应用场景。

select

select只能应用于channel的操作,既可以用于channel的数据接收,也可以用于channel的数据发送。

如果select的多个分支都满足条件,则会随机的选取其中一个满足条件的分支, 如语言规范中所说:

If multiple cases can proceed, a uniform pseudo-random choice is made to decide which single communication will execute.

`case`语句的表达式可以为一个变量或者两个变量赋值。

default语句。

下面的代码是 go by example 上的例子:

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
     
package main  
import "time"
import "fmt"
func main() {
c1 := make( chan string)
c2 := make( chan string)
go func() {
time.Sleep(time.Second * 1)
c1 <- "one"
}()
go func() {
time.Sleep(time.Second * 2)
c2 <- "two"
}()
for i := 0; i < 2; i++ {
select {
case msg1 := <-c1:
fmt.Println( "received", msg1)
case msg2 := <-c2:
fmt.Println( "received", msg2)
}
}
}

switch

switch可以为各种类型进行分支操作, 设置可以为接口类型进行分支判断(通过i.(type))。

switch 分支是顺序执行的,这和select不同。

     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
     
package main
import "fmt"
import "time"
func main() {
i := 2
fmt.Print( "Write ", i, " as ")
switch i {
case 1:
fmt.Println( "one")
case 2:
fmt.Println( "two")
case 3:
fmt.Println( "three")
}
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println( "It's the weekend")
default:
fmt.Println( "It's a weekday")
}
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println( "It's before noon")
default:
fmt.Println( "It's after noon")
}
whatAmI := func(i interface{}) {
switch t := i.( type) {
case bool:
fmt.Println( "I'm a bool")
case int:
fmt.Println( "I'm an int")
default:
fmt.Printf( "Don't know type %T\n", t)
}
}
whatAmI( true)
whatAmI (1)
whatAmI( "hey")
}

原文地址:http://colobu.com/2017/07/07/select-vs-switch-in-golang/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值