当前位置:网站首页>golang中的select详解
golang中的select详解
2022-07-01 17:49:00 【IT工作者】
注意
监听的case中,没有满足条件的就阻塞
多个满足条件的就任选一个执行
select本身不带循环,需要外层的fo
default通常不用,会产生忙轮询
break只能跳出select中的一个case
加入了默认分支,那么无论涉及通道操作的表达式是否有阻塞,select语句都不会被阻塞。如果那几个表达式都阻塞了,或者说都没有满足求值的条件,那么默认分支就会被选中并执行。
如果没有加入默认分支,那么一旦所有的case表达式都没有满足求值条件,那么select语句就会被阻塞。直到至少有一个case表达式满足条件为止。
如果select语句发现同时有多个候选分支满足选择条件,那么它就会用一种伪随机的算法在这些分支中选择一个并执行。注意,即使select语句是在被唤醒时发现的这种情况,也会这样做
作用
go里面提供了一个关键字select,通过select可以监听channel上的数据流动
select的用法与switch语言非常类似,由select开始一个新的选择块,每个选择块条件由case语句来描述
与switch语句可以选择任何可使用相等比较的条件相比,select有比较多的限制,其中最大的一条限制就是每个case语句里必须是一个IO操作
for {
select {
case <-chan1:
//.....
case chan2<-1:
//....
default:
//都没成功,进入......
}
}
在一个select语句中,go语言会按顺序从头到尾评估每一个发送和接收的语句
如果其中的任意一语句可以继续执行(即没有被阻塞),那么就从哪些可以执行的语句中任意选择一条来使用
如果没有任意一条语句可以执行(即所有的通道都被阻塞),那么有两种可能的情况:
如果给出了default语句,那么就会执行default语句,同时程序的执行会从select语句后的语句中恢复
如果没有default语句,那么select语句将被阻塞,直到至少有一个通信可以进行下去
防止channel超时机制
有时候会出现协程阻塞的情况,那么我们如何避免这个情况?我们可以使用select来设置超时
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v:= <-c:
fmt.Println(v)
//5秒钟自动关闭,避免长时间超时
case <-time.After(5 * time.Second):
fmt.Println("timeout")
o<-true
break
}
}
}()
//有值就主协程走,主协程走完就都没了
<-o
fmt.Println("程序结束")
}
超时这种写法也常常用来设置定时执行某段函数
go func(){
select {
case <-time.After(5 * time.Second):
dosometing()
}
}
边栏推荐
- How to use JMeter function and mockjs function in metersphere interface test
- Detailed explanation of string's trim() and substring()
- SQL injection vulnerability (MySQL and MSSQL features)
- Radhat builds intranet Yum source server
- Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
- Integer array merge [JS]
- Unity3d extended toolbar
- ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
- Replace UUID, nanoid is faster and safer!
- The reviewboard has 500 errors when submitting a review. Solutions
猜你喜欢
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Vulnhub range hacksudo Thor
How to write good code - Defensive Programming Guide
Work and leisure suggestions of old programmers
vulnhub靶场-hacksudo - Thor
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
(17) DAC conversion experiment
Penetration practice vulnhub range Tornado
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
Vulnhub range hacker_ Kid-v1.0.1
随机推荐
Glidefast consulting was selected as the elite partner of servicenow in 2022
About selenium element positioning being overwritten
C language implementation of sum of two numbers [easy to understand]
Work and leisure suggestions of old programmers
期货先锋这个软件正规吗安全吗?选择哪家期货公司更安全?
(十七)DAC转换实验
Key points on February 15, 2022
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
[C supplement] [string] display the schedule of a month by date
New patent applications and transfers
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
剑指 Offer 20. 表示数值的字符串
PETRv2:一个多摄像头图像3D感知的统一框架
Sword finger offer 20 String representing numeric value
线上开通ETF基金账户安全吗?有哪些步骤?
中国PBAT树脂市场预测及战略研究报告(2022版)
网上股票开户安全吗?是否可靠?
Penetration practice vulnhub range Nemesis
Thinkphp6 - CMS multi wechat management system source code