dragon456101 2013-05-10 03:12
浏览 863
已采纳

如何使用fmt在Go中打印出常量uint64?

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条回答 默认 最新

  • dongyu2764 2013-05-10 04:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?