In the Go Language specification under operators (
https://golang.org/ref/spec#Operators), there are a couple examples that
demonstrate this exact situation:
var u2 = 1<<s != 1.0 // illegal: 1 has type float64, cannot shift
var v float32 = 1<<s // illegal: 1 has type float32, cannot shift
The corresponding rule behind this is:
If the left operand of a non-constant shift expression is an untyped
constant, it is first converted to the type it would assume if the shift
expression were replaced by its left operand alone.
So in your baz() example, because 1 is an untyped constant, the shift
expression acts as if 1 is the only operand and converts it to a float64
type; therefore, 1 has type float64 and you cannot shift. To get around
this, just use an explicitly typed constant.
On Thu, Oct 13, 2016 at 7:37 PM, Carl Mastrangelo <
> In the program above, foo and bar compile but baz does not. It fails with
> the message: "invalid operation: 1 << b (shift of type float64)". This
> seems to be wrong on the surface, since the order of operations should
> imply the shift takes precedence. In the bar function, using a temporary
> variable without specifying the type shows that the compiler does know what
> to do. What am I missing?
> 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 golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups
To unsubscribe from this group and stop receiving emails from it, send an email
For more options, visit https://groups.google.com/d/optout.