当前位置:网站首页>What are the consequences of closing the read / write channel?
What are the consequences of closing the read / write channel?
2022-07-04 19:58:00 【Brother Xiaokun】
stay go In the interview of , Undoubtedly, the most frequently asked knowledge point is channel 了 .
When channel What happens when you read data after closing , Most often asked .
One 、 natural channel
First, let's start with a piece of normal operation code :
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
for item := range ch {
fmt.Println(item)
}
This code should be very familiar , Is there a problem with this writing ?
The result of execution :
$ go run n.go
1
2
3
fatal error: all goroutines are asleep - deadlock!
If one channel If it is not closed in a certain cooperation process , our for range The error of deadlock will be reported .
Two 、 Close and then read
1、 Use for range Read closed channel
Now we are for range Close this before channel, See what happens :
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
// If it is not closed, a deadlock will be reported fatal error: all goroutines are asleep - deadlock!
for item := range ch {
fmt.Println(item)
}
What's wrong with writing code like this ?
$ go run n.go
1
2
3
There's nothing wrong , Normal reading .
2、 Direct independent value
Now let's change the way of direct independent value :
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
item, state := <-ch // state Whether to read the value
fmt.Println(item, state)
state It will return whether the value has been obtained , The execution result of this code is to get the first value :
$ go run n.go
1 true
What if we read it repeatedly ?
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
<-ch
<-ch
<-ch
item, state := <-ch // state Whether to read the value
fmt.Println(item, state) // After reading all the values , You can't report an error if you read it again , You will only get zero
I read before 3 Time , After reading the data inside , Read it again, and it will be zero .
$ go run n.go
0 false
So the conclusion is : If channel There are elements that have not been read , Will read it correctly , Even if he has closed .
3、 ... and 、 Write the value inside ?
Finally, let's take a look at , When channel After closing , Write the value in it ?
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
ch <- 4
The result of this code execution :
$ go run n.go
panic: send on closed channel
Will be submitted to the panic, We can see the specific reasons go Source code , Path is :src/runtime/chan.go

边栏推荐
猜你喜欢

Online text line fixed length fill tool

HMM隐马尔可夫模型最详细讲解与代码实现

Siemens HMI download prompts lack of panel image solution

BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)

Creation of JVM family objects

Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?

应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设

Pointnet/Pointnet++点云数据集处理并训练

西门子HMI下载时提示缺少面板映像解决方案

多表操作-内连接查询
随机推荐
Dark horse programmer - software testing - stage 07 2-linux and database -09-24-linux command learning steps, wildcards, absolute paths, relative paths, common commands for files and directories, file
node_ Exporter deployment
勾股数规律(任意三个数能够满足勾股定理需要满足的条件)
HDU 6440 2018中国大学生程序设计网络选拔赛
How test engineers "attack the city" (Part 2)
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
Chrome development tool: what the hell is vmxxx file
Educational Codeforces Round 22 E. Army Creation
求2的n次方
BCG 使用之CBCGPProgressDlg进度条使用
解密函数计算异步任务能力之「任务的状态及生命周期管理」
矩阵翻转(数组模拟)
Matrix flip (array simulation)
Kotlin classes and objects
Some thoughts on whether the judgment point is located in the contour
1005 spell it right (20 points) (pat a)
New wizard effect used by BCG
Chrome开发工具:VMxxx文件是什么鬼
English语法_名词 - 使用
BCG 使用之新建向导效果