This is an code sample from the book Concurrency in Go. Inside a select block is the following statement
case takeStream <- <- valueStream:
I can't figure out what the double arrows do, and there's no explanation in the text. The output changes when I replace it with
case takeStream <- valueStream:
so it's clearly necessary
Complete function:
func take(done<- chan interface{}, valueStream <- chan interface{}, num int) <- chan interface{}{
takeStream := make ( chan interface{})
go func() {
defer close(takeStream)
for i := 0; i < num; i ++ {
select {
case <- done :
return
case takeStream <- <- valueStream:
}
}
}()
return takeStream
}
Edited: If I understand correctly, the statement expanded out would be
i := 5
valueStream <- i
tmp <- valueStream
takeStream <- tmp
so
takeStream <- <- valuesStream
is a shortcut
That explains why when I called
fmt.Println(<-takeStream)
I got funky numbers - presumably some numeric representation of the valueStream
Thanks!
takeStream <- <- valueStream
is a concatenation of a receive operation (receive operator) and a send statement.
The receive operator is a unary operator and as such, has highest precedence (Spec: Operators), and the send statement is a statement and falls outside of the operator hierarchy. As a consequence, c1 <- <- c2
is the same as c1 <- (<-c2)
.
So your example is the same as:
takeStream <- (<-valueStream)
(Note that any other interpretation wouldn't make any sense.)
And Spec: Send statements:
Both the channel and the value expression are evaluated before communication begins.
The value to be sent must be evaluated before it can be sent. So your example first receives a value from valueStream
, and then sends that value on takeStream
...
...Or so it would if this send statement (and receive operator) would stand on its own.
When used inside a select
statement as one of the communication op of a case
, then this is what happens:
Quoting from 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.
[...]
So when you have this:
select {
case <- done :
return
case takeStream <- <- valueStream:
}
Then <-valueStream
is evaluated (a value is received from valueStream
). If the operation is blocking, then the whole select
will block (even if done
is closed and thus ready to receive from).
Once a value is received from valueStream
, only then will it be decided if that value can be sent on takeStream
, and whether this case
is chosen if other cases can also proceed.
- 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.
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