当前位置:网站首页>[golang] use select {}
[golang] use select {}
2022-07-29 01:51:00 【JavaPub-rodert】
Blogger introduction : We-Media JavaPub Independent maintainer , Fans all over the Internet 15w+,csdn Blogger 、java Quality creators in the field ,51ctoTOP10 Blogger , You know / Nuggets / Hua Wei Yun / Alibaba cloud /InfoQ And other high-quality authors 、 Focus on Java、
GoLangTechnical fields and sidelines .GoLang More convenient to learn , One article in hand , I have
GoLang interview | GoLang interview | GoLang interview
scene
Sometimes we need to keep the application from quitting , Always running in the background .
Where is the effect of permanent blockage ? When we develop a program ,main The function must not exit before the subprocess finishes its work , Otherwise, all sub processes will be forced to quit . The service was forced to stop .
You usually use channel, Here's an introduction select{}
Code
func main() {
for i := 0; i < 10; i++ {
// start-up 20 A coroutine processes messages in the message queue
c := consumer.New()
go c.Start()
}
select {
} // Blocking
}
Explain
select yes Go A control structure in , Similar to... For communication switch sentence .
select Random execution A runnable case. without case Can run , It will block , Until there is case Can run . A default clause should always run .
package main
import "fmt"
func main() {
ch1 := make(chan int, 1)
ch2 := make(chan int, 1)
ch1 <- 1
ch2 <- 1
select {
case <-ch1:
fmt.Println("ch1 pop one element")
case <-ch2:
fmt.Println("ch2 pop one element")
/* You can define any number of case */
default: /* Optional */
fmt.Println("default! ")
}
}

Infinite loop
select{
}

边栏推荐
- 承办首届算力大会,济南胜在何处?
- 【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
- ELS stop at all
- 剑指offer专项突击版第13天
- TDA75610-I2C-模拟功放I2C地址的确定
- Embedded sharing collection 23
- It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
- 【7.21-26】代码源 - 【体育节】【丹钓战】【最大权值划分】
- We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
- Comprehensive upgrade, all you can imagine is here -- JD API interface
猜你喜欢

JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
![[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]](/img/0b/052d66712b987e3507aa6dfadcabe4.png)
[7.21-26] code source - [sports festival] [Dan fishing war] [maximum weight division]

【GoLang】网络连接 net.Dial

【流放之路-第八章】

Leetcode 112: path sum

StoneDB 邀请您参与开源社区月会!

使用POI,实现excel文件导出,图片url导出文件,图片和excel文件导出压缩包

【Web技术】1395- Esbuild Bundler HMR

golang启动报错【已解决】

【公开课预告】:快手GPU/FPGA/ASIC异构平台的应用探索
随机推荐
StoneDB 邀请您参与开源社区月会!
Reinforcement learning (I): Q-learning, with source code interpretation
【搜索】—— 迭代加深/双向DFS/IDA*
[netding cup 2020 rosefinch group]nmap
[web technology] 1395 esbuild bundler HMR
[7.21-26] code source - [good sequence] [social circle] [namonamo]
Code reading - ten C open source projects
560 and K
MySQL execution order
LeetCode 113:路径总和 II
The new generation of public chain attacks the "Impossible Triangle"
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
[hcip] OSPF experiment under mGRE environment, including multi process bidirectional republication and OSPF special area
ELS square movement
ELS new box falls
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
Reinforcement learning (II): SARS, with code rewriting
In depth analysis of C language memory alignment
How to choose professional, safe and high-performance remote control software
Data security is a competitive advantage. How can companies give priority to information security and compliance
