当前位置:网站首页>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()
}
}
边栏推荐
- June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
- Mysql database - Advanced SQL statement (2)
- EasyCVR设备录像出现无法播放现象的问题修复
- Radhat builds intranet Yum source server
- Penetration practice vulnhub range Tornado
- The difference and relationship between iteratible objects, iterators and generators
- Sword finger offer 20 String representing numeric value
- New patent applications and transfers
- 剑指 Offer 20. 表示数值的字符串
- DNS
猜你喜欢

Wechat applet blind box - docking wechat payment

Oom caused by improper use of multithreading

The difference and relationship between iteratible objects, iterators and generators

Length of learning and changing

vulnhub靶场-Hacker_Kid-v1.0.1

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

Encryption and decryption of tinyurl in leetcode

DNS

Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse

Penetration practice vulnhub range Nemesis
随机推荐
Software construction scheme of smart factory collaborative management and control application system
How to write good code - Defensive Programming Guide
How to use JMeter function and mockjs function in metersphere interface test
深度优先遍历和广度优先遍历[通俗易懂]
New patent applications and transfers
Thinkphp6 - CMS multi wechat management system source code
SQL injection vulnerability (MySQL and MSSQL features)
Intelligent operation and maintenance practice: banking business process and single transaction tracking
传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
Length of learning and changing
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
To improve the efficiency of office collaboration, trackup may be the best choice
MES production equipment manufacturing execution system software
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Euler function: find the number of numbers less than or equal to N and coprime with n
At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
APK签名流程介绍[通俗易懂]
New 95 community system whole station source code
Replace UUID, nanoid is faster and safer!
Relationship between sensor size, pixel, dpi resolution, inch and millimeter