使用Go检查通道是否具有即读值

如何查看频道是否有值得我阅读?

阅读频道时我不想阻止。我想看看它是否有价值。如果它有一个,我会读它。如果没有一个(还),我会做一些其他的,再次检查。

谢谢!

警告:这不再准确,请看下面的答案。

从文档:

If a receive expression is used in an
assignment or initialization of the
form

06000

the receive operation becomes
non-blocking. If the operation can
proceed, the boolean variable ok will
be set to true and the value stored in
x; otherwise ok is set to false and x
is set to the zero value for its type

转载注明原文:使用Go检查通道是否具有即读值 - 代码日志