当前位置:网站首页>GO语言-select语句
GO语言-select语句
2022-06-28 03:40:00 【一边学习一边哭】
前言
select是go语言当中提供的一个选择语句。select的语法类似switch语句,也属于控制语句。
那为什么select语句我们没有放在和if、switch语句一起?
因为select是配合channel通道使用的。每个 case 必须是一个通信操作,要么是发送要么是接收。
特性总结
- select只能用于channel操作,每个case都必须是一个channel;
- 如果有多个case可以允许(channel没有阻塞),则随机选择一条case语句执行;
- 如果没有case语句可以执行(channel发生阻塞),切没有default语句,则select语句会阻塞;
- 如果没有case语句可以执行(channel发生阻塞),有default语句,则执行default语句;
- 一般使用超时语句代替default语句;
- 如果case语句中的channel为nil,则忽略该分支,相当于从select中删除了该分支;
- 如果select语句在for循环中,一般不使用default语句,因为会引起CPU占用过高问题。
select语句
select语法格式
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* 你可以定义任意数量的 case */
default : /* 可选 */
statement(s);
}
示例
select只能用于channel操作,每个case都必须是一个channel。且channel是读取/写入都可以。
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: //读取channel数据
fmt.Println("ch1中的数据是:", num1)
case ch2 <- "201": //channel写入数据
fmt.Println("ch2有数据写入")
}
}
随机性
如果多条case语句都可以执行,则随机执行一条。
示例中,两条case语句都可以执行。多次执行可以发现,case语句是随机执行其中一条的。
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中的数据是:", num1)
case num2 := <-ch2:
fmt.Println("ch2中的数据是:", num2)
}
}
死锁
如果所有case语句都被阻塞,切没有default语句和超时语句,则程序会报错死锁。
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case <-ch1:
fmt.Println("ch1")
case <-ch2:
fmt.Println("ch2")
}
}
default语句
如果所有case语句都阻塞,有default语句的话,就会执行default语句。
要注意如果select语句在for循环中,default语句可能会造成CPU占用过高。
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case num1 := <-ch1:
fmt.Println("ch1中的数据是:", num1)
case num2 := <-ch2:
fmt.Println("ch2中的数据是:", num2)
default:
fmt.Println("通道阻塞...default")
}
}
超时语句
一般使用超时语句代替default语句。
func main() {
ch1 := make(chan int)
ch2 := make(chan int)
select {
case num1 := <-ch1:
fmt.Println("ch1中的数据是:", num1)
case num2 := <-ch2:
fmt.Println("ch2中的数据是:", num2)
case <-time.After(3 * time.Second):
fmt.Println("timeout...")
}
}
边栏推荐
- AS 3744.1标准中提及ISO8191测试,两者测试一样吗?
- 光的粒子说(光电效应/康普顿效应)
- The operating mechanism of spectrogram in audio Science
- 利用 telegraf influxdb grafana 搭建服务器监控平台
- Meichuang data security management platform has obtained the evaluation certificate of "data security product capability verification plan" of the Institute
- 指针链表
- Does the applet input box flash?
- 关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
- Introduction notes to machine learning
- Conversion between decimal and BCD codes in C language
猜你喜欢
Reading notes of top performance version 2 (II) -- CPU monitoring
Leetcode: monotonic stack structure (Advanced)
Pychart shares third-party modules among different projects
Introduction notes to machine learning
Open the field of maker education and creation
Arrangement of basic electrical knowledge (II)
歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
Adder - Notes
Notes to friendship chain
Cannot edit in read-only editor if it appears in vscode
随机推荐
C语言十进制与BCD码的相互转换
用一个栈实现另一个栈的排序
English grammar_ Adjective / adverb Level 3 - Comparative_ Useful Expressions
How the uni app automatically switches the requested address according to the environment
从零到一,教你搭建「以文搜图」搜索服务(一)
Several ways of sharing printers in LAN
音频 scipy 中 spectrogram 的运作机制
Web APIs DOM event foundation dark horse programmer
What is the process of en 1101 flammability test for curtains?
[MySQL] multi table connection query
使用tensorboard进行loss可视化
Detailed explanation of KVM common commands
设计一个有getMin功能的栈
04 summary of various query operations and aggregation operations of mongodb
How to write anti shake throttling for small programs?
Several important physical concepts
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
Open the field of maker education and creation
Pychart shares third-party modules among different projects
《性能之巅第2版》阅读笔记(二)--性能观察工具