当前位置:网站首页>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...")
}
}
边栏推荐
- Analyzing the comprehensive application ability of educational robot
- Learning notes of digital circuit (II)
- 美创入选“2022 CCIA中国网络安全竞争力50强”榜单
- 抖音实战~取关博主
- GenICam GenTL 标准 ver1.5(2)
- 利用ELK 搭建日志分析系统(一)—— 组件介绍
- Several important physical concepts
- Market competitiveness of robot programming education
- 关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
- 等保2.0密码要求是什么?法律依据有哪些?
猜你喜欢

Visualization of loss using tensorboard

Building a server monitoring platform with telegraf influxdb grafana

品达通用权限系统(Day 5~Day 6)

Lingge leangoo agile Kanban tool adds the functions of exporting card documents and pasting shared brain map nodes

A preliminary study of blackbody radiation

02 mongodb data types, important concepts and common shell instructions

ambari SSLError: Failed to connect. Please check openssl library versions.

Genicam gentl standard ver1.5 (2)

Pinda general permission system (day 5~day 6)

La norme européenne en 597 - 1 pour les meubles est - elle la même que les deux normes en 597 - 2 pour les ignifuges?
随机推荐
Secouer le son et se battre ~ prêter attention au blogueur
MSC 307(88) (2010 FTPC Code) Part 5低播焰测试
Conversion between decimal and BCD codes in C language
从零到一,教你搭建「以文搜图」搜索服务(一)
GO语言-select语句
光伏板怎么申请ASTM E108阻燃测试?
Introversion, lying flat and midlife crisis
使用tensorboard进行loss可视化
ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
Reading notes of top performance version 2 (II) -- Performance observation tool
leetcode:494. All methods of adding and subtracting operators to the array to get the specified value
抖音实战~关注博主
回溯—迷宫问题
有关函数模板的那些小知识-.-
揭开SSL的神秘面纱,了解如何用SSL保护数据
等保三级密码复杂度是多少?多久更换一次?
MySQL master-slave replication, separation and resolution
利用 telegraf influxdb grafana 搭建服务器监控平台
03 summary of various additions, updates and deletions of mongodb documents
第一章 Bash 入门