当前位置:网站首页>Go language -select statement
Go language -select statement
2022-06-28 04:12:00 【Crying while learning】
Preface
select yes go A choice statement provided in the language .select The syntax of is similar to switch sentence , It also belongs to the control statement .
What then? select The statement we didn't put in and if、switch Statement together ?
because select It's cooperation channel The channel uses . Every case It has to be a communication operation , Either send or receive .
Feature summary
- select It can only be used for channel operation , Every case Must be a channel;
- If there are more than one case It is permissible to (channel There's no obstruction ), Then select one at random case Statement execution ;
- without case Statement can execute (channel happening ), Cut no default sentence , be select Statements block ;
- without case Statement can execute (channel happening ), Yes default sentence , execute default sentence ;
- Generally, timeout statements are used instead of default sentence ;
- If case Statement channel by nil, Then ignore the branch , It's equivalent to from select The branch was deleted in ;
- If select Statements in for In circulation , Generally not used default sentence , Because it will cause CPU The problem of high occupancy .
select sentence
select Grammar format
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* You can define any number of case */
default : /* Optional */
statement(s);
}
Example
select It can only be used for channel operation , Every case Must be a channel. And channel Is read / Writing is OK .
func main() {
ch1 := make(chan int)
ch2 := make(chan string)
go func() {
ch1 <- 100
}()
go func() {
num2 := <-ch2
fmt.Println(num2)
}()
select {
case num1 := <-ch1: // Read channel data
fmt.Println("ch1 The data in is :", num1)
case ch2 <- "201": //channel Write data
fmt.Println("ch2 There's data written in ")
}
}
Randomness
If more than one case Statements can be executed , Then execute one randomly .
Example , Two article case Statements can be executed . Multiple executions can reveal ,case Statements are executed at random .
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
go func() {
ch1 <- 100
}()
go func() {
ch2 <- 200
}()
select {
case num1 := <-ch1:
fmt.Println("ch1 The data in is :", num1)
case num2 := <-ch2:
fmt.Println("ch2 The data in is :", num2)
}
}
Deadlock
If all case Statements are blocked , Cut no default Statement and timeout statement , The program will report an error deadlock .
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case <-ch1:
fmt.Println("ch1")
case <-ch2:
fmt.Println("ch2")
}
}
default sentence
If all case Statements are blocked , Yes default In words , Will execute default sentence .
Pay attention to if select Statements in for In circulation ,default Statements may cause CPU Occupy too much .
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case num1 := <-ch1:
fmt.Println("ch1 The data in is :", num1)
case num2 := <-ch2:
fmt.Println("ch2 The data in is :", num2)
default:
fmt.Println(" The channel is blocked ...default")
}
}
Timeout statement
Generally, timeout statements are used instead of default sentence .
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case num1 := <-ch1:
fmt.Println("ch1 The data in is :", num1)
case num2 := <-ch2:
fmt.Println("ch2 The data in is :", num2)
case <-time.After(3 * time.Second):
fmt.Println("timeout...")
}
}
边栏推荐
- Secouer le son et se battre ~ prêter attention au blogueur
- 多项目设计开发·类库项目引入入门
- Analyzing the comprehensive application ability of educational robot
- How to learn a programming language systematically| Dark horse programmer
- 使用tensorboard进行loss可视化
- From zero to one, I will teach you to build a "search by text and map" search service (I)
- Leetcode: monotonic stack structure (Advanced)
- MSC 307(88) (2010 FTPC Code) Part 9床上用品试验
- How to apply for ASTM E108 flame retardant test for photovoltaic panels?
- Uncover the mystery of SSL and learn how to protect data with SSL
猜你喜欢
歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
Chapter 1 Introduction to bash
Talking about cloud primitiveness, we have to talk about containers
Reading notes of top performance version 2 (II) -- CPU monitoring
Analysis of future teacher research ability under steam education framework
2021年终总结及2022年展望
Pychart shares third-party modules among different projects
2022年6月对自己近况的一次总结
Learning notes of digital circuit (II)
Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
随机推荐
美创入选“2022 CCIA中国网络安全竞争力50强”榜单
Zipkin service link tracking
【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
A preliminary study of blackbody radiation
领歌leangoo敏捷看板工具新增导出卡片文档和粘贴共享脑图节点功能
The operating mechanism of spectrogram in audio Science
利用 telegraf influxdb grafana 搭建服务器监控平台
仅用递归函数和栈操作逆序一个栈
Analyzing the comprehensive application ability of educational robot
applicationContext. Getbeansoftype obtains the execution methods of all implementation classes under an interface or obtains the operation application scenarios such as implementation class objects. L
歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
GCD maximum common divisor
leetcode - 329. 矩阵中的最长递增路径
Introduction notes to machine learning
Sorting from one stack to another
僅用遞歸函數和棧操作逆序一個棧
第一个.net core MVC项目
Introversion, lying flat and midlife crisis
月赛补题
上线MES系统后,企业发生了这些变化......