当前位置:网站首页>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

边栏推荐
- 牛客小白月赛7 谁是神箭手
- C language - Introduction - Foundation - grammar - process control (VII)
- Upgrade the smart switch, how much is the difference between the "zero fire version" and "single fire" wiring methods?
- Is it safe to open an account at Great Wall Securities? How to open an account when buying stocks
- 黑马程序员-软件测试--09阶段2-linux和数据库-31-43修改文件权限字母发的说明,-查找链接修改文件,查找文件命令,链接文件,压缩解压方式,vi编辑器基本使用,
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
- Pytoch learning (4)
- 【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案
- Swagger突然发癫
- 牛客小白月赛7 E Applese的超能力
猜你喜欢
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Pytorch学习(四)
Actual combat simulation │ JWT login authentication
西门子HMI下载时提示缺少面板映像解决方案
ACM组合计数入门
92. (cesium chapter) cesium building layering
C language - Introduction - Foundation - grammar - process control (VII)
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
记一次 .NET 某工控数据采集平台 线程数 爆高分析
How to use async Awati asynchronous task processing instead of backgroundworker?
随机推荐
安徽 中安在线文旅频道推出“跟着小编游安徽”系列融媒体产品
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
Chrome development tool: what the hell is vmxxx file
Kotlin basic data type
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
1005 Spell It Right(20 分)(PAT甲级)
项目中遇到的线上数据迁移方案1---总体思路整理和技术梳理
Educational Codeforces Round 22 E. Army Creation
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
Niuke Xiaobai monthly race 7 I new Microsoft Office Word document
Actual combat simulation │ JWT login authentication
1002. A+B for Polynomials (25)(PAT甲级)
BCG 使用之CBCGPTabWnd控件(相当于MFC TabControl)
双冒号作用运算符以及命名空间详解
1006 Sign In and Sign Out(25 分)(PAT甲级)
C语言-入门-基础-语法-流程控制(七)
Comment utiliser async awati asynchrone Task Handling au lieu de backgroundworker?
如何使用Async-Awati异步任務處理代替BackgroundWorker?
Master the use of auto analyze in data warehouse
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,