相关疑难解决方法(0)

make(chan bool)与make(chan bool,1)的行为有何不同?

我的问题来自尝试阅读一个频道,如果可以的话,或者如果可以的话,使用一个select声明来写它.

我知道指定的通道make(chan bool, 1)是缓冲的,我的问题的一部分是它之间的区别,并且make(chan bool)- 这个页面所说的是同样的东西make(chan bool, 0)- 一个可以适合0值的通道的点是什么它?

操场A:

chanFoo := make(chan bool)

for i := 0; i < 5; i++ {
    select {
    case <-chanFoo:
        fmt.Println("Read")
    case chanFoo <- true:
        fmt.Println("Write")
    default:
        fmt.Println("Neither")
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

Neither
Neither
Neither
Neither
Neither
Run Code Online (Sandbox Code Playgroud)

(删除default案例会导致死锁!!)

现在看到游乐场B:

chanFoo := make(chan bool, 1)   // the only difference is the buffer size of 1

for i := 0; i …
Run Code Online (Sandbox Code Playgroud)

channel go

11
推荐指数
1
解决办法
5831
查看次数

标签 统计

channel ×1

go ×1