1

In Golang

why var y = int(0.1) have a error but x := 0.1; y := int(x) is ok?

ymy
  • 27
  • 3
  • 9
    The first one is converting a constant, the other one a variable. The are different rules when converting contants. https://golang.org/ref/spec#Conversions **Constants:** `int(1.2) // illegal: 1.2 cannot be represented as an int` **Non-Constant:** x can be converted to type T if `x's type and T are both integer or floating point types.` – mkopriva Sep 11 '19 at 10:13
  • 6
    See related / possible duplicate: [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 11 '19 at 10:15
  • 2
    Possible duplicate of [GO lang - constant truncated to integer](https://stackoverflow.com/questions/16151199/go-lang-constant-truncated-to-integer) – Shudipta Sharma Sep 11 '19 at 10:58

0 Answers0