mediumConceptCodingDesignMidSenior
Several producers write to one channel and one consumer reads it. Who closes the channel?
In Go, the sender closes a channel when no more values will be sent; receivers do not close it just because they are done. Multiple senders need coordination so close happens exactly once after all sends finish. Use this channels answer to show the decision, trade-off, and evidence rather than a memorised definition. It also connects goroutines to the point an interviewer is testing.
4 min readUpdated 2026-08-15
Answer
Short answer
The goroutine that owns sending is responsible for closing the channel. With many producers, use a WaitGroup or coordinator to close after all producers exit, and use context cancellation when receivers stop early.
Was this answer helpful?
Likely follow-ups
- What happens if a second goroutine closes the same channel?
- How do you signal to the producers that the consumer has given up?
- Why can the receiver not simply close when it has what it needs?
- What does the two-value receive form tell you that the one-value form does not?
Related questions
- How do goroutines leak, and how would you find a leak in a running service?mediumAlso on goroutines and channels5 min
- A flash sale decrements stock from many threads. Do you go optimistic or pessimistic, and how do you prove nothing oversold?mediumAlso on concurrency5 min
- How would you design a thread-safe component, and why is adding synchronized to every method not a design?hardAlso on concurrency7 min
- Build an in-memory pub-sub with topics, many subscribers per topic and at-least-once delivery. How does a message get redelivered?hardAlso on concurrency6 min