如何使用fmt在Go中打印出常量uint64? [英] How can I print out an constant uint64 in Go using fmt?

查看:363
本文介绍了如何使用fmt在Go中打印出常量uint64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过:

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

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

但是我收到以下错误消息:

but I got the following error message:

常数18446744073709551615溢出int

constant 18446744073709551615 overflows int

我该如何解决?谢谢!

推荐答案

math.MaxUint64是常量,而不是int64.尝试尝试:

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

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

这里的问题是常量没有类型.该常数将根据其使用的上下文采用一种类型.在这种情况下,它被用作接口{},因此编译器无法知道您要使用哪种具体类型.对于整数常量,默认为int.由于您的常量溢出一个int,因此这是一个编译时错误.通过传递uint64(num),您是在通知编译器您希望将该值视为uint64.

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.

请注意,此特定常数仅适用于uint64,有时仅适用于uint.该值甚至大于标准int64可以容纳的值.

Note that this particular constant will only fit in a uint64 and sometimes a uint. The value is even larger than a standard int64 can hold.

这篇关于如何使用fmt在Go中打印出常量uint64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆