使用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

http://stackoverflow.com/questions/3398490/checking-if-a-channel-has-a-ready-to-read-value-using-go

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:使用Go检查通道是否具有即读值