4 ms·
Go doesn't have non-blocking receives anymore. value, ok := <- ch will block, always. Iff the channel is closed, ok will be false, allowing you to distingui
by Jabbles 14y ago
Go doesn't have non-blocking receives anymore.
value, ok := <- ch
will block, always. Iff the channel is closed, ok will be false, allowing you to distinguish between a zero value sent on the channel, and the zero value you get back if you try to receive on a closed channel. Receiving from a nil channel always blocks.
Select statements allow you to check if a channel has had a value sent on it.
http://golang.org/ref/spec#Receive_operator http://golang.org/ref/spec#Receive_operator
https://groups.google.com/d/msg/golang-nuts/Z63l4LDOlsI/54uTd4x9eiIJ https://groups.google.com/d/msg/golang-nuts/Z63l4LDOlsI/54uT...
http://golang.org/ref/spec#Select_statements http://golang.org/ref/spec#Select_statements
- deleted 14y ago[deleted]
- crawshaw 14y agoFor those who didn't dive into the spec, the non-blocking recieve syntax has changed to use the select statement: func f(ch chan int) { select { case v := <-ch: fmt.Println("got", v) default: fmt.Println("did not block") } } http://play.golang.org/p/ql0qSUVXeX http://play.golang.org/p/ql0qSUVXeX