小编典典

如何将 uint64 转换为字符串

go

我正在尝试使用我使用的方法进行打印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


阅读 692

收藏
2021-12-27

共1个答案

小编典典

strconv.Itoa()需要一个 type 值int,所以你必须给它:

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

但是要知道,如果int是 32 位(而uint6464位),这可能会失去精度,而且符号也不同。strconv.FormatUint()会更好,因为它需要一个类型的值uint64

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

如果您的目的只是打印值,则无需将其转换为 toint或 to string,请使用以下之一:

log.Println("The amount is:", charge.Amount)
log.Printf("The amount is: %d\n", charge.Amount)
2021-12-27