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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘