不关闭管道会怎么样?

主动关闭管道是个好习惯,但是向关闭的管道推送会造成panic,所以我的想法是不关闭管道就行了。那么不关闭管道会引起什么不良的后果吗?
已邀请:

astaxie - 创造、获取、分享、传播和应用Go

赞同来自: Beanbee Charlotte tonyluj wy ddxx11223

可以的,当你channel不用的时候GC会回收。

我之前在两个地方看到过这个设计的说法:

1. Lan

> Note that it is only necessary to close a channel if the receiver is looking for a close. Closing the channel is a control signal on the channel indicating that no more data follows.
https://groups.google.com/forum/#!msg/golang-nuts/pZwdYRGxCIk/qpbHxRRPJdUJ

2. The Go Programming Language

> You needn't close every channel when you've finished with it. It's only necessary to close a channel when it is important to tell the receiving goroutines that all data have been sent. A channel that the garbage collector determinies to be unreachable will have its resources reclaimed whether or not it is closed. (Don't confuse this with the close operation for open files. It is important to call the Close method on every file when you've finished with it.)

要回复问题请先登录注册