ZhangZhihui's Blog  

Reading from a closed channel returns the zero value of its data type. However, if you try to write to a closed channel, your program is going to crash in a bad way (panic).

 

func main() {
    ch := make(chan int, 10)
    ch <- -1
    ch <- 1
    <-ch
    <-ch
    close(ch)
    read := <-ch
    fmt.Println(read)
    ch <- 10
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
0
panic: send on closed channel

goroutine 1 [running]:
main.main()
        /zdata/Github/ztest/main.go:16 +0xdc
exit status 2

 

posted on 2024-06-13 19:36  ZhangZhihuiAAA  阅读(5)  评论(0编辑  收藏  举报