2
var x uint64 = 257
var y int = 257
fmt.Println("rv1 is ", byte(x))        // ok
fmt.Println("rv2 is ", byte(y))        // ok
fmt.Println("rv3 is ", byte(257))      // constant 257 overflows byte
fmt.Println("rv4 is ", byte(int(257))) // constant 257 overflows byte 

It is strange.

All of them are converting int to byte,so all of them should be error.

But case 1,2 is ok!

How could be that?

Peter
  • 29,454
  • 5
  • 48
  • 60
binbin
  • 167
  • 2
  • 11
  • 8
    That's how the language works. Please consult the Tour of Go or the Language Spec on constants. I hope you know that 257 does overflow a byte. – Volker Sep 29 '17 at 12:02
  • @Volker Yes,I know 257 does overflow a byte.So case 1,2 ok make me confuse – binbin Sep 29 '17 at 12:14
  • 1
    The comment in the code is confusing. All four are overflows, but the last two were detected during compilation (as pointed by @Volker, consult language spec). The first two (`rv1` and `rv2`) are also overflow (at run-time), so it's not *ok*. – putu Sep 29 '17 at 12:16
  • 1
    Possible duplicate of [Does go compiler's evaluation differ for constant expression and other expression](https://stackoverflow.com/questions/39444852/does-go-compilers-evaluation-differ-for-constant-expression-and-other-expressio/39445372#39445372). – icza Sep 29 '17 at 12:17
  • 1
    @putu What is your definition of "not ok" ? The OP simply asks why the last 2 cases results in a compiler error, while the first 2 cases does not result in any error (and both will print the number 1) – nos Sep 29 '17 at 12:28
  • @nos, Yes, I understand the question, but when comparing the comments (first two lines, and last two lines), what came to my mind are: *the last two are overflows*, while the first two are "ok" (the meaning is too broad/confusing). – putu Sep 29 '17 at 12:52

1 Answers1

9

Variable numeric values can be converted to smaller types, with the normal loss of the high bits.

The compiler refuses to do this for constant values (that is clearly always an error). This is required by the spec (emphasize mine):

Every implementation must:

  • Represent integer constants with at least 256 bits.
  • Represent floating-point constants, including the parts of a complex constant, > with a mantissa of at least 256 bits and a signed binary exponent of at least 16 bits.
  • Give an error if unable to represent an integer constant precisely.
  • Give an error if unable to represent a floating-point or complex constant due to overflow.
  • Round to the nearest representable constant if unable to represent a floating-point or complex constant due to limits on precision.

These requirements apply both to literal constants and to the result of evaluating constant expressions.

Consequently, if you change var x and var y to const x and const y, you get an error for all four cases.

Peter
  • 29,454
  • 5
  • 48
  • 60