dousong5492 2017-01-22 05:18 采纳率: 100%
浏览 4435
已采纳

在Golang中将uint64转换为字符串

我正试图打印 stringuint64 但是我使用的strconv方法的组合是无效的。

log.Println("The amount is: " + strconv.Itoa((charge.Amount)))

给我的提示是:

cannot use charge.Amount (type uint64) as type int in argument to strconv.Itoa

那么我该如何打印 string这个呢?

  • 写回答

5条回答 默认 最新

  • dongwu1992 2017-01-22 05:23
    关注

    strconv.Itoa() expects a value of type int, so you have to give it that:

    log.Println("The amount is: " + strconv.Itoa(int(charge.Amount)))
    

    But know that this may lose precision if int is 32-bit (while uint64 is 64), also sign-ness is different. strconv.FormatUint() would be better as that expects a value of type uint64:

    log.Println("The amount is: " + strconv.FormatUint(charge.Amount, 10))
    

    For more options, see this answer: Golang: format a string without printing?

    If your purpose is to just print the value, you don't need to convert it, neither to int nor to string, use one of these:

    log.Println("The amount is:", charge.Amount)
    log.Printf("The amount is: %d
    ", charge.Amount)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料