4 ms·
Ok I think I'm not understanding this correctly then. Why does this return an error? package main import ( "fmt" ) const tst = 10
by friseurtermin 6y ago
Ok I think I'm not understanding this correctly then. Why does this return an error?
package main
import (
"fmt"
)
const tst = 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
func main() {
fmt.Println("%v", tst)
}
Error:
./prog.go:16:17: constant 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 overflows int
See: https://play.golang.org/p/47l5qAsXD5r https://play.golang.org/p/47l5qAsXD5r
- howeyc 6y agohttps://golang.org/ref/spec#Constants https://golang.org/ref/spec#Constants > An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required. The default type for a number (integer) is int. If you were to add a period in that long string of zeros, the default for floating-point is float64.
- HALtheWise 6y agoIf you look closely, that error happens at the print line statement, not at the constant declaration. The constant is perfectly legal to exist as written, and you can even do constant operations like mathematics on it, you just can't legally assign it to a variable (implicitly in this case as part of the function call) if it's value is too large for the type of that variable.
- friseurtermin 6y agoI see, that makes sense. Thanks!