当前位置:网站首页>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()
}
}
边栏推荐
- 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
- Encryption and decryption of tinyurl in leetcode
- Common design parameters of solid rocket motor
- Unity3d extended toolbar
- MySQL + JSON = King fried
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- 开发那些事儿:EasyCVR集群设备管理页面功能展示优化
- Redis -- data type and operation
- 两数之和c语言实现[通俗易懂]
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
猜你喜欢

How to write good code - Defensive Programming Guide

ACL 2022 | decomposed meta learning small sample named entity recognition

How to use JMeter function and mockjs function in metersphere interface test

Work and leisure suggestions of old programmers

Apache iceberg source code analysis: schema evolution

Replace UUID, nanoid is faster and safer!

Detailed explanation of string's trim() and substring()

【Try to Hack】vulnhub DC4

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

多线程使用不当导致的 OOM
随机推荐
多线程使用不当导致的 OOM
ArrayList扩容详解
The difference and relationship between iteratible objects, iterators and generators
MFC obtains local IP (used more in network communication)
Penetration practice vulnhub range Tornado
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
DNS
(1) CNN network structure
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Cassette helicopter and alternating electric field magnetic manometer DPC
vulnhub靶场-Hacker_Kid-v1.0.1
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
Research Report on development prediction and investment direction of nylon filament sewing thread in China (2022 Edition)
PHP implements sensitive word filtering system "suggestions collection"
(16) ADC conversion experiment
Sword finger offer II 105 Maximum area of the island
深度优先遍历和广度优先遍历[通俗易懂]
MySQL + JSON = King fried
开发那些事儿:EasyCVR平台添加播放地址鉴权