Search code examples
gobroadcastshutdownchannel

Close golang channel with multiple values


Imagine I have one sender and arbitrary number of receivers connected via a channel. When the sender finishes it closes the channel and all receivers get notified. So far so good.

Now imagine sender wants receivers to finish in two different ways, say, "close immediately" and "first finish what you are doing, then close". To achieve that it has to broadcast two different values (NOW vs. LATER) to all receivers when shutting down.

What is the Go-idiomatic way to do this?


Solution

  • Personally I'd use two channels to signal shutdown: https://play.golang.org/p/5erPW1tFXp