Consider this program (https://play.golang.org/p/V0fu9rD8_D)
package main
import (
"fmt"
)
func main() {
c := make(chan int, 1)
c <- 1
d := <-chan int(c)
fmt.Printf("%T\n", d)
e := (<-chan int)(c)
fmt.Printf("%T\n", e)
}
Its output is
int
<-chan int
Intuitively it seems ok (and I believe the output is correct). Let's look
closer on the RHS expression of line
d := <-chan int(c)
>From the specs:
Expression <https://golang.org/ref/spec#Expression> = UnaryExpr
<https://golang.org/ref/spec#UnaryExpr> | Expression
<https://golang.org/ref/spec#Expression> binary_op
<https://golang.org/ref/spec#binary_op> Expression
<https://golang.org/ref/spec#Expression> .
UnaryExpr <https://golang.org/ref/spec#UnaryExpr> = PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> | unary_op
<https://golang.org/ref/spec#unary_op> UnaryExpr
<https://golang.org/ref/spec#UnaryExpr> .
unary_op <https://golang.org/ref/spec#unary_op> = "+" | "-" | "!" | "^" |
"*" | "&" | "<-" .
PrimaryExpr <https://golang.org/ref/spec#PrimaryExpr> = Operand
<https://golang.org/ref/spec#Operand> | Conversion
<https://golang.org/ref/spec#Conversion> | PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> Selector
<https://golang.org/ref/spec#Selector> | PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> Index
<https://golang.org/ref/spec#Index> | PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> Slice
<https://golang.org/ref/spec#Slice> | PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> TypeAssertion
<https://golang.org/ref/spec#TypeAssertion> | PrimaryExpr
<https://golang.org/ref/spec#PrimaryExpr> Arguments
<https://golang.org/ref/spec#Arguments> .
Conversion <https://golang.org/ref/spec#Conversion> = Type
<https://golang.org/ref/spec#Type> "(" Expression
<https://golang.org/ref/spec#Expression> [ "," ] ")" .
Type <https://golang.org/ref/spec#Type> = TypeName
<https://golang.org/ref/spec#TypeName> | TypeLit
<https://golang.org/ref/spec#TypeLit> | "(" Type
<https://golang.org/ref/spec#Type> ")" .
TypeLit <https://golang.org/ref/spec#TypeLit> = ArrayType
<https://golang.org/ref/spec#ArrayType> | StructType
<https://golang.org/ref/spec#StructType> | PointerType
<https://golang.org/ref/spec#PointerType> | FunctionType
<https://golang.org/ref/spec#FunctionType> | InterfaceType
<https://golang.org/ref/spec#InterfaceType> | SliceType
<https://golang.org/ref/spec#SliceType> | MapType
<https://golang.org/ref/spec#MapType> | ChannelType
<https://golang.org/ref/spec#ChannelType> .
ChannelType <https://golang.org/ref/spec#ChannelType> = ( "chan" | "chan"
"<-" | "<-" "chan" ) ElementType <https://golang.org/ref/spec#ElementType> .
It seems to me that parsing `<-chan int(c)` as
a receive operation from a conversion of `c` to type `chan int`
is as valid as parsing it as
a conversion of `c` to type `<-chan int`.
In the later case the program would output
<-chan int
<-chan int
I must be missing something. Which rule selects the first parse? Can
anybody please enlighten me? Thanks in advance.
--
-j
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.