【问题标题】:Why I do get Go error "panic: strconv: illegal AppendInt/FormatInt base"为什么我会收到 Go 错误“恐慌:strconv:非法 AppendInt/FormatInt 基数”
【发布时间】:2016-07-06 19:45:18
【问题描述】:

我不知道为什么在运行时会收到此错误消息。即使使用最简单的单行代码也会触发: strconv.FormatUint(uint64(123), 64)

我在这里理解错了吗?代码编译得很好。

编辑:找到了解决方案(正如 Tim Cooper 稍后指出的那样)。我认为the examples in the documentation 令人困惑:

s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

当您在示例中仅使用基数 64 和 16 时,很容易得出错误的结论。但是,现在我知道出了什么问题,错误消息就更有意义了。

【问题讨论】:

  • 关于您的编辑:文档中没有 64 用于基础;它只用作位大小。
  • 如果您想要 base64,就像在非二进制安全环境中表示二进制数据的方式一样,encoding/base64 为您提供。
  • 上例中的浮点数是否也是位大小?我对问题进行了更多编辑,以使其更加明显,我指的是文档的哪一部分。很抱歉造成混乱。

标签: go casting panic strconv


【解决方案1】:

不支持 Base 64,如 the documentation 中所述:

func FormatUint(i uint64, base int) string

FormatUint 返回给定基数中 i 的字符串表示形式,即 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 2013-09-03
    • 1970-01-01
    • 2019-05-10
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    相关资源
    最近更新 更多