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

边栏推荐
- Educational codeforces round 22 E. Army Creation
- English grammar_ Noun - use
- 1007 Maximum Subsequence Sum(25 分)(PAT甲级)
- . Net ORM framework hisql practice - Chapter 2 - using hisql to realize menu management (add, delete, modify and check)
- Reflection (I)
- Abc229 summary (connected component count of the longest continuous character graph in the interval)
- FPGA timing constraint sharing 01_ Brief description of the four steps
- Chrome开发工具:VMxxx文件是什么鬼
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
猜你喜欢

Lenovo explains in detail the green smart city digital twin platform for the first time to solve the difficulties of urban dual carbon upgrading

多表操作-内连接查询

实战模拟│JWT 登录认证

C语言-入门-基础-语法-流程控制(七)

Introduction to ACM combination counting

abc229 总结(区间最长连续字符 图的联通分量计数)
牛客小白月赛7 谁是神箭手

Master the use of auto analyze in data warehouse

【问题】druid报异常sql injection violation, part alway true condition not allow 解决方案

In operation (i.e. included in) usage of SSRs filter
随机推荐
C language - Introduction - Foundation - grammar - process control (VII)
欧拉函数
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
Lm10 cosine wave homeopathic grid strategy
Introduction to ACM combination counting
node_ Exporter deployment
kotlin 条件控制
1011 World Cup betting (20 points) (pat a)
黑马程序员-软件测试--07阶段2-linux和数据库-09-24-linux命令学习步骤,通配符,绝对路径,相对路径,文件和目录常用命令,文件内容相关操作,查看日志文件,ping命令使用,
HDU 6440 2018 Chinese college student program design network competition
HDU 1097 A hard puzzle
记一次 .NET 某工控数据采集平台 线程数 爆高分析
数据集划分
Creation of JVM family objects
1005 spell it right (20 points) (pat a)
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
Functional interface
Add namespace declaration
Explore the contour drawing function drawcontours() of OpenCV in detail with practical examples
Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)