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

前端 未结 1 763
被撕碎了的回忆
被撕碎了的回忆 2021-02-12 16:22

I tried:

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

but I got the following error message:

constant 1844674407370955

1条回答
  •  孤街浪徒
    2021-02-12 16:46

    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. The value is even larger than a standard int64 can hold.

    0 讨论(0)
提交回复
热议问题