当前位置:网站首页>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
边栏推荐
- c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
- Mysql database basic operation -ddl | dark horse programmer
- "Only one trip", active recommendation and exploration of community installation and maintenance tasks
- 1008 elevator (20 points) (PAT class a)
- Online data migration scheme encountered in the project 1 - general idea sorting and technical sorting
- Siemens HMI download prompts lack of panel image solution
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- 1006 sign in and sign out (25 points) (PAT class a)
- Kotlin inheritance
- HMM hidden Markov model and code implementation
猜你喜欢
English语法_名词 - 使用
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
Chrome开发工具:VMxxx文件是什么鬼
Dark horse programmer - software testing - 09 stage 2-linux and database -31-43 instructions issued by modifying the file permission letter, - find the link to modify the file, find the file command,
C # use stopwatch to measure the running time of the program
Siemens HMI download prompts lack of panel image solution
记一次 .NET 某工控数据采集平台 线程数 爆高分析
mysql中explain语句查询sql是否走索引,extra中的几种类型整理汇总
Multi table operation - external connection query
FPGA timing constraint sharing 01_ Brief description of the four steps
随机推荐
矩阵翻转(数组模拟)
Educational Codeforces Round 22 E. Army Creation
Niuke Xiaobai month race 7 who is the divine Archer
Allure of pytest visual test report
Cbcgpprogressdlg progress bar used by BCG
Cbcgpprogressdlgctrl progress bar used by BCG
HDU 1097 A hard puzzle
水晶光电:长安深蓝SL03的AR-HUD产品由公司供应
Personal thoughts on Architecture Design (this article will be revised and updated continuously later)
Niuke Xiaobai month race 7 F question
1011 World Cup betting (20 points) (pat a)
How test engineers "attack the city" (Part 2)
C# 使用StopWatch测量程序运行时间
多表操作-内连接查询
Online sql to excel (xls/xlsx) tool
BCG 使用之新建向导效果
Euler function
Educational Codeforces Round 22 E. Army Creation
Mysql database basic operation -ddl | dark horse programmer
kotlin 继承