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

边栏推荐
- HCIA configuration instance (ENSP)
- 知道创宇上榜CCSIP 2022全景图多个领域
- MPEG音频编码三十年
- [7.21-26] code source - [good sequence] [social circle] [namonamo]
- 【7.21-26】代码源 - 【平方计数】【字典序最小】【“Z”型矩阵】
- 规划数学期末模拟考试一
- Leetcode 112: path sum
- How to protect WordPress website from network attack? It is essential to take safety measures
- ELS stop at all
- 【10点公开课】:快手GPU/FPGA/ASIC异构平台的应用探索
猜你喜欢

10 major network security incidents in the past 10 years

Reinforcement learning (I): Q-learning, with source code interpretation

【Unity项目实践】合成大西瓜

【Web技术】1395- Esbuild Bundler HMR
![[网鼎杯 2020 朱雀组]Nmap](/img/22/1fdf716a216ae26b9110b2e5f211f6.png)
[网鼎杯 2020 朱雀组]Nmap

Analysis of Multi Chain use cases on moonbeam -- review of Derek's speech in Polkadot decoded 2022

matplotlib中文问题

With the explosive growth of digital identity in 2022, global organizations are facing greater network security

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

Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
随机推荐
JS事件简介
Cross modal alignment 20220728
Leetcode 113: path sum II
[WesternCTF2018]shrine
【流放之路-第六章】
[search] - iteration deepening / bidirectional dfs/ida*
Platofarm community ecological gospel, users can get premium income with elephant swap
Analyze OP based on autoware_ global_ Planner global path planning module re planning
Regular filtering data learning notes (①)
【流放之路-第七章】
PCL point cloud intensity image
Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
mysql的执行顺序
Moonbeam上的多链用例解析——Derek在Polkadot Decoded 2022的演讲文字回顾
Top network security prediction: nearly one-third of countries will regulate blackmail software response within three years
Come on, handwritten RPC S2 serialization exploration
More interesting Title Dynamic Effect
Practical experience of Google cloud spanner
Read the recent trends of okaleido tiger and tap the value and potential behind it
【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”
