By convention, who is responsible for closing a channel, and why should receivers never close a channel?
Last updated
The sender closes the channel because only it knows no more values are coming. Closing lets receivers drain remaining values and end a
rangeloop (or getok == false).Receivers must not close: sending on a closed channel panics, and so does closing twice. A receiver also can't know if other senders are still active.
With multiple senders, use a
sync.WaitGroupand a separate goroutine that closes after all finish. Closing is only needed to signal receivers (GC frees channels anyway).