Is it OK to leave a channel open?

GoChannel

Go Problem Overview


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?

func (requestCh chan<- Request) GetResponse(data RequestData) Response {
    reply := make(chan Response)
    requestCh <- Request{data: data, replyCh: reply}
    return <-reply
}

Go Solutions


Solution 1 - Go

It's OK to leave a Go channel open forever and never close it. When the channel is no longer used, it will be garbage collected.

> 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. > > Design Question: Channel Closing

Solution 2 - Go

Yes, it is ok to keep a channel open. As the go programming language book stated:

> 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.)

Solution 3 - Go

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.

Solution 4 - Go

"One general principle of using Go channels is don't close a channel from the receiver side and don't close a channel if the channel has multiple concurrent senders."

As clearly mentioned in answer above that every channel will be GCed eventually once it is marked for cleanup, so it is okay to leave channel un-closed the only difference it will make is that that channel will be available for gc after a few cycles maybe if not closed explicitly.

Also the following articles this and this shows various ways to close a channel in case of 1:N, N:1 or M:N (senders:receivers)

Solution 5 - Go

This is very well covered above, however I find the following from A Tour of Go very clear, which also gives an example of when to close:

> Another note: Channels aren't like files; you don't usually need to close them. Closing is only necessary when the receiver must be told there are no more values coming, such as to terminate a range loop.

Solution 6 - Go

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKluygView Question on Stackoverflow
Solution 1 - GopeterSOView Answer on Stackoverflow
Solution 2 - GocizixsView Answer on Stackoverflow
Solution 3 - GoSoniaView Answer on Stackoverflow
Solution 4 - GoAbhishek SrivastavaView Answer on Stackoverflow
Solution 5 - Gouser11809641View Answer on Stackoverflow
Solution 6 - GoŁukasz GrunerView Answer on Stackoverflow