How to print persistent uint64 in Go using fmt?

I tried:

fmt.Printf ("% d", math.MaxUint64)

but I got the following error message:

constant 18446744073709551615 overflow int

How can i fix this? Thanks!

+8
go printing int64
source share
1 answer

math.MaxUint64 is a constant, not int64. Try instead:

 fmt.Printf("%d", uint64(num)) 

The problem here is that the constant is untyped. The constant will take on a type depending on the context in which it is used. In this case, it is used as the {} interface, so the compiler does not know which specific type you want to use. For integer constants, it defaults to int . Since your constant overflows int, this is a compile-time error. By uint64(num) , you tell the compiler that you want the value to be treated as uint64 .

Note that this particular constant will only match uint64, and sometimes uint. It is even larger than standard int64.

+20
source share

All Articles