To range over a channel in Go, write for v := range ch. The loop receives values until the channel is closed and drained, then exits on its own. To detect closure outside a loop, use the two value receive v, ok := <-ch, where ok is false once the channel is closed and empty. This is how go range over channel code normally ends a consumer without extra signalling.
Only the sender should close a channel, and only when no more values will be sent. Closing is a signal to receivers, not a way to free memory.
A for range loop over a channel receives one value per iteration. It blocks while the channel is empty and open, and it stops when the channel is closed and every buffered value has been received.
The goroutine sends three values and closes the channel with defer close(ch). The range loop in main ends after the third value, so done is printed. Without the close, the loop would wait forever and the program would stop with fatal error: all goroutines are asleep - deadlock!.
The receive expression can return a second boolean value.
A closed channel still delivers buffered values first, with ok true. After it is empty, every receive returns immediately with the zero value of the element type and ok false. Without the second value you cannot tell a real zero from closure, which matters when zero is valid data such as 0 or an empty string.
send on closed channel.range loop.isClosed check.If many goroutines send on one channel, none of them should close it. Let a coordinator wait for all of them with sync.WaitGroup and close once.
Since Go 1.22 each loop iteration has its own id variable, so the closure captures the right value. In older versions you had to pass id as an argument. The closer goroutine calls Wait and then close, so the range loop in main can finish.
Sometimes you want to stop early. range cannot also watch a context, so use select in a plain loop and check ok.
Both exits matter. ctx.Done() handles cancellation, and !ok handles a producer that finished. Forgetting the second case makes the loop spin on a closed channel, because a receive from a closed channel is always ready.
Go 1.23 added range over function iterators, such as iter.Seq. They are separate from channels, but they are often a better fit for sequences produced in one goroutine because they avoid goroutines and closing rules. Use channels when values really cross goroutine boundaries.
No. It blocks while the channel is open and empty. It stops only after the channel is closed and all buffered values are consumed.
It blocks forever, because receiving from a nil channel never completes. That is a common cause of a goroutine leak.
It should not. The sender would panic on its next send. Use a separate done channel or a context.Context to signal the sender to stop.
Not for memory. It is only a bug if a consumer waits for closure, for example in a range loop.
Use for v := range ch to consume values until the channel is closed and drained, and v, ok := <-ch to detect closure explicitly. Let the sender close the channel, and use a WaitGroup plus a closer goroutine when there are several senders. Add select with ctx.Done() when the consumer needs cancellation, and remember that sending on or closing a closed channel panics.