简体   繁体   English

关闭具有多个值的golang通道

[英]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. 为了实现这一点,必须在关闭时向所有接收器广播两个不同的值(NOW vs. LATER)。

What is the Go-idiomatic way to do this? Go惯用的方式是什么?

我个人将使用两个通道来发出关闭信号: https : //play.golang.org/p/5erPW1tFXp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM