当前位置:网站首页>Detailed explanation of select in golang
Detailed explanation of select in golang
2022-07-01 17:54:00 【It workers】
Be careful
Monitoring case in , If the conditions are not met, it will block
If multiple conditions are met, choose one to execute
select It doesn't have a loop , Requiring an outer layer fo
default Usually not , Busy polling will occur
break Can only jump out of select One of them case
Added default branch , So whether the expression involving channel operation is blocked or not ,select Statements are not blocked . If those expressions are blocked , Or it doesn't meet the evaluation conditions , Then the default branch will be selected and executed .
If no default branch is added , So once all case None of the expressions satisfy the evaluation condition , that select The statement will be blocked . Until at least one of them case Until the expression satisfies the condition .
If select Statement finds that multiple candidate branches meet the selection criteria at the same time , Then it will use a pseudo-random algorithm to select one of these branches and execute . Be careful , Even if select The statement is found when awakened , It will do the same
effect
go It provides a keyword select, adopt select Can monitor channel Data flow on
select The usage and switch The language is very similar , from select Start a new selection block , Each selection block condition is defined by case To describe
And switch Statement can select any condition that can use equality comparison ,select There are more restrictions , One of the biggest restrictions is that each case There must be a IO operation
for {
select {
case <-chan1:
//.....
case chan2<-1:
//....
default:
// It didn't work , Get into ......
}
}In a select In the sentence ,go The language evaluates each statement sent and received from beginning to end in sequence
If any of these statements can continue ( That is, it is not blocked ), Then select any one of the executable statements to use
If no statement can be executed ( That is, all channels are blocked ), So there are two possibilities :
If given default sentence , Then it will carry out default sentence , At the same time, the execution of the program will start from select Restore... In the statement after the statement
without default sentence , that select The statement will be blocked , Until at least one communication can go on
prevent channel Timeout mechanism
Sometimes there will be co process blocking , So how can we avoid this situation ? We can use select To set the timeout
func main() {
c := make(chan int)
o := make(chan bool)
go func() {
for {
select {
case v:= <-c:
fmt.Println(v)
//5 Seconds automatically turn off , Avoid long timeouts
case <-time.After(5 * time.Second):
fmt.Println("timeout")
o<-true
break
}
}
}()
// If it's worth it, let's go , When the main cooperation process is finished, it's gone
<-o
fmt.Println(" Program end ")
}Timeout is often used to set the timing of the execution of certain functions
go func(){
select {
case <-time.After(5 * time.Second):
dosometing()
}
}
边栏推荐
- Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
- 官宣!香港科技大学(广州)获批!
- How to write good code - Defensive Programming Guide
- Depth first traversal and breadth first traversal [easy to understand]
- APK签名流程介绍[通俗易懂]
- 传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- [beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
- Redis主从实现10秒检查与恢复
- About selenium element positioning being overwritten
猜你喜欢

String的trim()和substring()详解

Gameframework eating guide

ISO 27001 Information Security Management System Certification

Penetration practice vulnhub range Tornado
![[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON](/img/83/9bd9ce7608ebfe7207ac008b9e8ab1.png)
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON

DNS
![Integer array merge [JS]](/img/0d/70535e0eb1c299bda25159b58c70d7.png)
Integer array merge [JS]

Kia recalls some K3 new energy with potential safety hazards

Good looking UI mall source code has been scanned, no back door, no encryption

ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
随机推荐
Is the software of futures pioneer formal and safe? Which futures company is safer to choose?
Glidefast consulting was selected as the elite partner of servicenow in 2022
Kia recalls some K3 new energy with potential safety hazards
Sword finger offer II 105 Maximum area of the island
2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
Htt [ripro network disk link detection plug-in] currently supports four common network disks
Roewe rx5's "a little more" product strategy
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
February 16, 2022 Daily: graph neural network self training method under distribution and migration
About selenium element positioning being overwritten
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
MES production equipment manufacturing execution system software
Development cost of smart factory management system software platform
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
Cassette helicopter and alternating electric field magnetic manometer DPC
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
Yyds dry inventory MySQL RC transaction isolation level implementation
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
中国茂金属聚乙烯(mPE)行业研究报告(2022版)