Is it OK to leave a channel open?

后端 未结 5 1155
伪装坚强ぢ
伪装坚强ぢ 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 01:48

    Yes, it's OK to leave the channel open, and in fact it is typical. A channel being open does not constitute a reference to the channel object, and so does not keep it from being garbage collected.

提交回复
热议问题