I am curious why the following does not work. In general select
with a default:
prevents deadlock, but not in this case:
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")
}
}
Obviously it doesn't like the <- <-
but I'm wondering what's going on behind the surface here. In other situations <- <-
is allowed (though perhaps not recommended).
Now, a selection can be made directly in the channel itself using any of the selection tools or brushes. To finalize a selection, press Ctrl and click on the copied channel. The selected area will be represented by "marching ants". With the stripe selected, right-click inside of the selection and choose Fill.
To do this, click and drag the channel down to the plus ( + ) icon in the bottom-right corner. Now, a selection can be made directly in the channel itself using any of the selection tools or brushes. To finalize a selection, press Ctrl and click on the copied channel. The selected area will be represented by "marching ants".
Simply right click on the Channel you want to duplicate and select Duplicate. In this case I select and duplicate the Blue Channel. This duplicate channel is actually a selection. You can go to Select, Load Selection and load Blue copy.
We'll start from the following as a base code: The code: select_base.go The code creates a new channel with make (chan val-type). Channels are typed by the values they convey.
a <- <- b
is the same as a<- (<-b)
, because the <-
operator associates with the leftmost chan
possible.
So the select
has a case
with a send operation (in the form of a<- (something)
). And what happens here is that the right-hand-side expression of the send statement (the value to be sent) is evaluated first - which is <-b
. But this will block forever (because no one is sending anything on b
), so:
fatal error: all goroutines are asleep - deadlock!
Relevant section form the Spec: Select statements:
Execution of a "select" statement proceeds in several steps:
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.
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.
...
So if default
is present, the select
does prevent blocking if none of the communications can proceed in step 2, but your code gets stuck in step 1.
Just to be complete, if there would be a goroutine that would send a value on b
, then evaluation of <- b
would not block, so the execution of select
would not stuck in step 2, and you would see the expected "select worked as naively expected"
(because receiving from a
could still not proceed therefore default
would be chosen):
go func() { b <- 1 }()
select {
// ...
}
Try it on the Go Playground.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With