Sending on the close channel

What if multiple goroutines write to the same channel? If one goroutine has done its work it can’t simply close the channel. Other goroutines may still need to write to the channel

There should always only be a single “owner” who is then responsible for closing the channel once all work has been done.

A sync.WaitGroup is often used to wait on all goroutines having finished their work, though other mechanisms are imaginable. Choose one that suits your needs.

1 Like

I recommend you two videos:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.