您可以将某些值存储在受其保护的全局变量中,以
RWMutex保持进度。生成器更新它。消费者阅读和表演。
您也可以对长度为1的通道进行非阻塞写入:
var c = make(chan struct{}, 1)select {case c <- struct{}{}:default:}这样,发送方要么向通道添加一个元素,要么如果通道已满就什么都不做。
读者将此空结构视为信号-它应在全局变量中获取更新的值。
另一种方式: 可更新的频道
var c = make(chan int, 1)select {case c <- value: // channel was empty - okdefault: // channel if full - we have to delete a value from it with some precautions to not get locked in our own channel switch { case <- c: // read stale value and put a fresh one c <- value default: // consumer have read it - so skip and not get locked }}


