String 如何将uint64转换为字符串

String 如何将uint64转换为字符串,string,go,type-conversion,strconv,String,Go,Type Conversion,Strconv,我正在尝试使用uint64打印字符串,但我使用的strconv方法组合不起作用 log.Println("The amount is: " + strconv.Itoa((charge.Amount))) 给我: 无法在strconv.Itoa的参数中将charge.Amount(类型uint64)用作int类型 如何打印此字符串?需要类型为int的值,因此您必须为其指定: log.Println("The amount is: " + strconv.Itoa(int(charge.Amou

我正在尝试使用
uint64
打印
字符串
,但我使用的
strconv
方法组合不起作用

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

无法在strconv.Itoa的参数中将charge.Amount(类型uint64)用作int类型

如何打印此
字符串

需要类型为
int
的值,因此您必须为其指定:

log.Println("The amount is: " + strconv.Itoa(int(charge.Amount)))
但是要知道,如果
int
是32位(而
uint64
是64位),那么这可能会失去精度,而且符号度也不同。更好,因为它需要类型为
uint64
的值:

log.Println("The amount is: " + strconv.FormatUint(charge.Amount, 10))
有关更多选项,请参见以下答案:

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

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


如果你真的想把它保存在一个字符串中,你可以使用一个Sprint函数。例如:

myString := fmt.Sprintf("%v", charge.Amount)

如果要将
int64
转换为
string
,可以使用:

strconv.FormatInt(time.Now().Unix(), 10)


如果您来这里是想了解如何将字符串转换为uint64,那么下面是如何完成的:

为什么不干脆
fmt.Sprint(charge.Amount)
strconv.FormatInt(time.Now().Unix(), 10)
strconv.FormatUint
newNumber, err := strconv.ParseUint("100", 10, 64)