2016-02-24 72 views
5

我很好奇爲什麼以下不起作用。一般來說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") 
    } 
} 

顯然它不喜歡的<- <-但我想知道這是怎麼回事的表面背後。在其他情況下<- <-被允許(儘管可能不推薦)。

回答

6

a <- <- ba<- (<-b)相同,因爲<-運營商與最左邊的chan可能關聯。

所以select有一個case與發送操作(的形式a<- (something))。這裏發生的是send語句的右側表達式(要發送的值)首先被計算 - 這是<-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存在,則select確實防止阻塞,如果沒有的通信可以在步驟2中繼續,但是您的代碼卡在步驟1中。


只要是完整的,是否會有這將在b發送的值,那麼<- b評價不會阻止的goroutine,所以select的執行將不會停留在步驟2中,和你會看到預期的"select worked as naively expected"(因爲從a接收仍然無法因此繼續default將選擇):

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

select { 
    // ... 
} 

嘗試它的Go Playground