当前位置:网站首页>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...")
}
}
边栏推荐
- Visualization of loss using tensorboard
- How to write anti shake throttling for small programs?
- ambari SSLError: Failed to connect. Please check openssl library versions.
- 05 MongoDB对列的各种操作总结
- 利用ELK 搭建日志分析系统(三)—— 安全认证
- What is the level 3 password complexity of ISO? How often is it replaced?
- Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
- 第一个.net core MVC项目
- 电学基础知识整理(一)
- MySQL master-slave replication, separation and resolution
猜你喜欢

Notes to friendship chain

Market competitiveness of robot programming education

Several ways of sharing printers in LAN

leetcode - 329. 矩阵中的最长递增路径

@Several scenarios of transactional failure

How to write a software test report? Here comes the third party performance report template

One article tells you what kubernetes is

光伏板怎么申请ASTM E108阻燃测试?

门级建模—学习笔记

MSc 307 (88) (2010 FTPC code) Part 2 smoke and toxicity test
随机推荐
ELK 搭建日志分析系统 + Zipkin服务链路追踪整合
谈云原生,不得不谈的容器
关于 SY8120I 的DC-DC的降压芯片的学习(12V降至3.3V)
Are the two flame retardant standards of European furniture en 597-1 and en 597-2 the same?
From zero to one, I will teach you to build a "search by text and map" search service (I)
MySQL master-slave replication, separation and resolution
错排兼排列组合公式
数字电路学习笔记(二)
基于正点原子stm32的mini板的TFTLCD显示实验
歐洲家具EN 597-1 跟EN 597-2兩個阻燃標准一樣嗎?
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?
Talking about cloud primitiveness, we have to talk about containers
Principle and Simulation of switching power supply buck circuit
Elk builds log analysis system + Zipkin service link tracking integration
How to write anti shake throttling for small programs?
Problems with cat and dog queues
MSc 307 (88) (2010 FTPC code) Part 5 low flame spread test
门级建模—学习笔记
Web APIs DOM event foundation dark horse programmer
Reverse a stack with recursive functions and stack operations only