当前位置:网站首页>[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{
}

边栏推荐
- StoneDB 为何敢称业界唯一开源的 MySQL 原生 HTAP 数据库?
- [hcip] two mGRE networks are interconnected through OSPF (ENSP)
- 【流放之路-第七章】
- 新生代公链再攻「不可能三角」
- [unity project practice] synthetic watermelon
- Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
- BOM系列之window对象
- 正则过滤数据学习笔记(①)
- How to protect WordPress website from network attack? It is essential to take safety measures
- 九天后我们一起,聚焦音视频、探秘技术新发展
猜你喜欢

MySQL execution order

Read the recent trends of okaleido tiger and tap the value and potential behind it

【Golang】- runtime.Goexit()

Timer of BOM series
![[hcip] MPLS Foundation](/img/91/a2aebf333fb3e3fedf0fc393e175a9.png)
[hcip] MPLS Foundation

MPEG音频编码三十年

Behind the second round of okaleido tiger sales is the strategic support of ecological institutions

Embedded sharing collection 23

采用QT进行OpenGL开发(二)绘制立方体

StoneDB 邀请您参与开源社区月会!
随机推荐
Analyze OP based on autoware_ global_ Planner global path planning module re planning
[网鼎杯 2020 朱雀组]Nmap
抓包工具Charles使用
规划数学期末模拟考试一
Comprehensive upgrade, all you can imagine is here -- JD API interface
【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
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.
Reinforcement learning (II): SARS, with code rewriting
About df['a column name'] [serial number]
Making high-precision map based on autoware (V)
围绕新市民金融聚焦差异化产品设计、智能技术提效及素养教育
Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
For a safer experience, Microsoft announced the first PC with a secure Pluto chip
[7.21-26] code source - [good sequence] [social circle] [namonamo]
Reinforcement learning (I): Q-learning, with source code interpretation
【HCIP】两个MGRE网络通过OSPF实现互联(eNSP)
【GoLang】网络连接 net.Dial
Basic label in body
numpy.where() 用法和np.argsort()的用法
【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域