我有一個正在偵聽TCP連接並將這些通道發送回主循環的去程序。我在做例行程序的原因是讓這個監聽不阻塞,並且能夠同時處理活動連接。從Go通道獲取價值
我有一個空的默認情況下,像這樣的select語句來實現這一點:
go pollTcpConnections(listener, rawConnections)
for {
// Check for new connections (non-blocking)
select {
case tcpConn := <-rawConnections:
currentCon := NewClientConnection()
pendingConnections.PushBack(currentCon)
fmt.Println(currentCon)
go currentCon.Routine(tcpConn)
default:
}
// ... handle active connections
}
這裏是我的pollTcpConnections常規:
func pollTcpConnections(listener net.Listener, rawConnections chan net.Conn) {
for {
conn, err := listener.Accept() // this blocks, afaik
if(err != nil) {
checkError(err)
}
fmt.Println("New connection")
rawConnections<-conn
}
}
的問題是,我從來沒有收到這些連接。如果我做一個堵的方式,像這樣:
for {
tcpConn := <-rawConnections
// ...
}
我收到的連接,但它會阻止......我試圖緩衝通道爲好,但同樣的事情發生。我在這裏錯過了什麼?
請參閱[這個問題](http://stackoverflow.com/questions/14633373/how-to-do-nothing-when-no-channel-is-ready-to-beread)爲答案。 – thwd