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

在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这个呢?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 uint64 打印 string 但我使用的 strconv 方法没有任何组合有效。

  log.Println(“数量为:” + strconv.Itoa((charge  .Amount))))
   
 
 

给我:

不能使用charge.Amount(类型uint64)作为类型 int in 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条)

相关推荐 更多相似问题