当前位置:网站首页>读写关闭的channel是啥后果?
读写关闭的channel是啥后果?
2022-07-04 17:32:00 【小锟哥哥】
在 go 的面试中,最常问到的知识点无疑是 channel 了。
当 channel 关闭后再去读取数据会出现啥情况,最经常被问到。
一、正常的 channel
首先我们先来一段正常操作的代码:
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
for item := range ch {
fmt.Println(item)
}
这段代码应该非常熟悉,这样写是否有问题呢?
执行后的结果:
$ go run n.go
1
2
3
fatal error: all goroutines are asleep - deadlock!
如果一个 channel 不在某个协程里面关闭的话,我们的 for range 就会报死锁的错误。
二、关闭后再读取
1、使用 for range 读取关闭后的 channel
现在我们在 for range 之前关闭下这个 channel,看会出现啥情况:
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
// 如果不关闭会报死锁 fatal error: all goroutines are asleep - deadlock!
for item := range ch {
fmt.Println(item)
}
这样写代码会出现啥问题呢?
$ go run n.go
1
2
3
啥错也没有,正常的读取。
2、直接独立取值
下面我们换直接独立取值的方式:
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
item, state := <-ch // state 为是否读到有值
fmt.Println(item, state)
state 会返回是否取到了值,这段代码的执行结果是取到第一个值:
$ go run n.go
1 true
如果我们反复读取呢?
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
<-ch
<-ch
<-ch
item, state := <-ch // state 为是否读到有值
fmt.Println(item, state) //全部读完值之后,再读也不会报错,只会取到零值
我前面读取 3 次,把里面的数据读取完毕后,再读取得到就是零值了。
$ go run n.go
0 false
所以结论是:如果 channel 有元素还未读,会正确读出来,哪怕他已经关闭了。
三、往里面写值呢?
最后一起来看下,当 channel 关闭后,往里面写值会怎样?
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
ch <- 4
这段代码执行后的结果:
$ go run n.go
panic: send on closed channel
会报 panic,具体原因我们可以看 go 的源码,路径为:src/runtime/chan.go

边栏推荐
- 如何使用 wget 和 curl 下载文件
- Caché WebSocket
- Is it safe to download the mobile version of Anxin securities and open an account online
- Machine learning concept drift detection method (Apria)
- 学习路之PHP--phpstudy创建项目时“hosts文件不存在或被阻止打开”
- Li Kou brush question diary /day7/6.30
- Scala basic tutorial -- 20 -- akka
- How is the entered query SQL statement executed?
- C language printing exercise
- Scala basic tutorial -- 12 -- Reading and writing data
猜你喜欢

Li Kou brush question diary /day4/6.26

神经网络物联网应用技术学什么

力扣刷题日记/day4/6.26

1、 Introduction to C language

Wireshark packet capturing TLS protocol bar displays version inconsistency

Lex and yacc based lexical analyzer + parser

Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines

力扣刷题日记/day5/2022.6.27

Rookie post station management system based on C language

千万不要只学 Oracle、MySQL!
随机推荐
Halcon模板匹配
英特尔集成光电研究最新进展推动共封装光学和光互连技术进步
SIGMOD’22 HiEngine论文解读
力扣刷题日记/day1/2022.6.23
输入的查询SQL语句,是如何执行的?
Li Kou brush question diary /day7/6.30
Learning path PHP -- phpstudy "hosts file does not exist or is blocked from opening" when creating the project
IBM WebSphere MQ检索邮件
Interpretation of SIGMOD '22 hiengine paper
Journal des problèmes de brosse à boutons de force / day6 / 6.28
神经网络物联网应用技术学什么
小发猫物联网平台搭建与应用模型
【OpenCV入门到精通之九】OpenCV之视频截取、图片与视频互转
Wireshark抓包TLS协议栏显示版本不一致问题
Scala基础教程--16--泛型
Scala基础教程--19--Actor
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Li Kou brush question diary /day6/6.28
repeat_P1002 [NOIP2002 普及组] 过河卒_dp
Scala基础教程--12--读写数据