select 频道 <- <- 频道

select with channel <- <- channel

我很好奇为什么以下不起作用。通常 selectdefault: 可以防止死锁,但在这种情况下不会:

package main

import "fmt"

func main () {
    a := make(chan int)
    b := make(chan int)

    select {
    case a <- <- b:
        fmt.Println("this is impossible")
    default:
        fmt.Println("select worked as naively expected")
    }
}

显然它不喜欢 <- <- 但我想知道表面背后发生了什么。在其他情况下 <- <- 是允许的(尽管可能不推荐)。

a <- <- ba<- (<-b) 相同,因为<- 运算符关联最左边的chan 可能。

所以 select 有一个带有发送操作的 case(以 a<- (something) 的形式)。这里发生的是发送语句右侧的表达式(要发送的值)首先被求值——即 <-b。但这将永远阻塞(因为没有人在 b 上发送任何东西),所以:

fatal error: all goroutines are asleep - deadlock!

相关部分形成 Spec: Select statements:

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.

  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.

  3. ...

因此,如果存在 default,如果 none 的通信可以在 步骤 2 中继续进行,select 确实可以防止阻塞,但是您的代码卡在 步骤 1.


为了完整起见, 如果有一个 goroutine 会在 b 上发送一个值,那么对 <- b 的求值就不会阻塞,所以 select 的执行不会停留在第 2 步,你会看到预期的 "select worked as naively expected" (因为从 a 接收仍然无法继续,因此 default 将被选择) :

go func() { b <- 1 }()

select {
    // ...
}

Go Playground 上试试。