Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert uint64 to string

I am trying to print a string with a uint64 but no combination of strconv methods that I use is working.

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

Gives me:

cannot use charge.Amount (type uint64) as type int in argument to strconv.Itoa

How can I print this string?

like image 219
Anthony Avatar asked Jan 22 '17 05:01

Anthony


1 Answers

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\n", charge.Amount) 
like image 90
icza Avatar answered Sep 18 '22 20:09

icza