Why wont this code compile?
package main
const a = 1.000001
const base = 0
const b = a+base
func main() {
f(b)
}
func f(int) {}
$ go run a.go
# command-line-arguments
./a.go:4: constant 1 truncated to integer
It's saying that 1 is truncated? Or that 1 cannot be truncated? Which 1 is it talking about?
Someone answered the above code doesn't compile because b
is a float64
. But then why does this compile:
package main
import "fmt"
const a = 1.000001
const b = a-0.000001
func main() {
fmt.Printf("%T %v\n",a,a)
fmt.Printf("%T %v\n",b,b)
f(b)
}
func f(int) {}
$ go run a.go
float64 1.000001
float64 1
? b
is a float64
here, but it can be passed to f
.
Your first program can be rewritten like this:
Which is clearly not passing an integer value to an integer function.
Your second program can similarly be rewritten like this:
Which looks fine.
All I did was manually substitute the
a
andb
constants. This is all go does.