How can I print out an constant uint64 in Go using fmt?

点点圈 提交于 2019-12-04 17:10:13

问题


I tried:

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

but I got the following error message:

constant 18446744073709551615 overflows int

How can I fix this? Thanks!


回答1:


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

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

The issue here is that the constant is untyped. The constant will assume a type depending on the context in which it is used. In this case, it is being used as an interface{} so the compiler has no way of knowing what concrete type you want to use. For integer constants, it defaults to int. Since your constant overflows an int, this is a compile time error. By passing uint64(num), you are informing the compiler you want the value treated as a uint64.

Note that this particular constant will only fit in a uint64 and sometimes a uint. It is even bigger than a standard int64.



来源:https://stackoverflow.com/questions/16474594/how-can-i-print-out-an-constant-uint64-in-go-using-fmt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!