Is it OK to leave a channel open?

后端 未结 5 1156
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 01:29

Is it OK to leave a Go channel open forever (never close the channel) if I never check for its state? Will it lead to memory leaks? Is the following code OK?



        
5条回答
  •  执念已碎
    2020-11-28 02:10

    Go is garbage collected, so you don't really have to 'free' anything.

    There is possibility to close channels, but it's mostly used as - close(channel) - tell the goroutine (or main program) that nothing else will be sent on that channel.

提交回复
热议问题