有没有办法检查Go通道中的值是否已缓冲?

huangapple go评论73阅读模式
英文:

Is there any way to check if values are buffered in a Go chan?

问题

我该如何实现类似以下的功能?:

func foo(input <-chan char, output chan<- string) {
    var c char
    var ok bool
    for {
        if input中有缓冲值 {
            c, ok = <-input
        } else {
            output <- "更新消息"
            c, ok = <-input
        }
        对c和ok进行操作
    }
}

基本上,我想要检查通道中是否有缓冲值,以便在线程被阻塞之前,如果没有缓冲值,我可以发送一条更新消息。

英文:

How can I do something like the following?:

func foo(input &lt;-chan char, output chan&lt;- string) {
    var c char
    var ok bool
    for {
        if ThereAreValuesBufferedIn(input) {
            c, ok = &lt;-input
        } else {
            output &lt;- &quot;update message&quot;
            c, ok = &lt;-input
        }
        DoSomethingWith(c, ok) 
    }
}

Basically, I want to check if there are buffered values in the chan so that if there aren't, I could send an update message before the thread is blocked.

答案1

得分: 3

是的,这就是select调用允许你做的事情。它可以让你检查一个或多个通道是否准备好读取值。

英文:

Yes, this is what the select call allows you to do. It will enable you to check one or more channels for values ready to be read.

答案2

得分: 3

package main

func foo(input <-chan char, output chan<- string) {
for {
select {
case c, ok := <-input:
if ok { // ThereAreValuesBufferedIn(input)
... process c
} else { // input is closed
... handle closed input
}
default:
output <- "update message"
c, ok := <-input // will block
DoSomethingWith(c, ok)
}
}
}

英文:
package main

func foo(input &lt;-chan char, output chan&lt;- string) {
        for {
                select {
                case c, ok := &lt;-input:
                        if ok { // ThereAreValuesBufferedIn(input)
                                ... process c
                        } else { // input is closed
                                ... handle closed input
                        }
                default:
                        output &lt;- &quot;update message&quot;
                        c, ok := &lt;-input // will block
                        DoSomethingWith(c, ok)
                }
                
        }
}

EDIT: Fixed scoping bug.

答案3

得分: 1

其他人已经回答了你关于代码的问题(使用select),但为了完整起见,并回答你问题标题中特定的问题(“有没有办法检查Go通道中的值是否已缓冲?”),lencap内置函数在缓冲通道上的工作方式与预期相同(len返回缓冲元素的数量,cap返回通道的最大容量)。

http://tip.golang.org/ref/spec#Length_and_capacity

英文:

Others have answered your question for what you wanted to do with your code (use a select), but for completeness' sake, and to answer the specific question asked by your question's title ("Is there any way to check if values are buffered in a Go chan?"), the len and cap built-in functions work as expected on buffered channels (len returns the number of buffered elements, cap returns the maximum capacity of the channel).

http://tip.golang.org/ref/spec#Length_and_capacity

huangapple
  • 本文由 发表于 2013年7月30日 23:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/17951344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定