当前位置:网站首页>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()
}
}
边栏推荐
- Irradiance, Joule energy, exercise habits
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
- Euler function: find the number of numbers less than or equal to N and coprime with n
- Pyqt5, draw a histogram on the control
- 深度优先遍历和广度优先遍历[通俗易懂]
- 中国茂金属聚乙烯(mPE)行业研究报告(2022版)
- [splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
- 2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
- Encryption and decryption of tinyurl in leetcode
- APK签名流程介绍[通俗易懂]
猜你喜欢
PETRv2:一个多摄像头图像3D感知的统一框架
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
Penetration practice vulnhub range Tornado
Kernel stray cat stray dog pet adoption platform H5 source code
How to write good code - Defensive Programming Guide
LeetCode中等题之TinyURL 的加密与解密
Why should you consider using prism
People help ant help task platform repair source code
Countdownlatch blocking wait for multithreading concurrency
随机推荐
Is online stock account opening safe? Is it reliable?
Common design parameters of solid rocket motor
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
Is it safe to open an ETF account online? What are the steps?
Vulnhub range hacksudo Thor
Alibaba cloud Li Feifei: China's cloud database has taken the lead in many mainstream technological innovations abroad
Is Huishang futures a regular futures platform? Is it safe to open an account in Huishang futures?
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
开发那些事儿:EasyCVR平台添加播放地址鉴权
EasyCVR设备录像出现无法播放现象的问题修复
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
Depth first traversal and breadth first traversal [easy to understand]
China acetonitrile market forecast and strategic consulting research report (2022 Edition)
China metallocene polyethylene (MPE) Industry Research Report (2022 Edition)
transform. Forward and vector3 Differences in the use of forward
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Irradiance, Joule energy, exercise habits
DNS
Openlayers 自定义气泡框以及定位到气泡框