Junior · Concurrency

Channel closure is a protocol

Who closes a channel used by several workers?

Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.

Reveal a strong answer
  1. Closing announces that no more values will be sent; it is not mandatory garbage-collection cleanup. Sending on a closed channel panics; receivers can drain buffered values and then observe ok == false.

  2. The owner that can prove all senders have finished closes once. With several producers, a coordinator waits for them before closing the results channel. A consumer cancels upstream work through a separate context or signal.

  3. A nil channel disables a select case but blocks outside select. Reading from a closed channel succeeds immediately, so a loop must exit or disable that case instead of spinning.

Common trap

A receiver closing the results channel because it has enough values can race with a still-active sender.

Try it

Design the ordering for three producers and one collector that can stop early.

Worked solution

Give producers a cancellation context, make sends selectable on cancellation, join all producers and close results from one coordinator. The collector may stop after cancellation, but senders must still be able to finish.

Change the constraint

How does the result change for a buffered channel that is closed while values remain?

How did you do?

Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.

Not marked. Progress stays on this browser, separate from course completion.

Primary references

Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.