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 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果