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)
Answer from icza on Stack Overflowstrconv.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)
if you want to convert int64 to string, you can use :
strconv.FormatInt(time.Now().Unix(), 10)
or
strconv.FormatUint
c++ - converting _int64 to a string - Stack Overflow
c - How to convert string to int64_t? - Stack Overflow
The risks of converting int64 to string using a combination of "int()" and "strconv.Itoa"
Convert Int64 to string
C++11 standardized the std::to_string function:
#include <string>
int main()
{
int64_t value = 128;
std::string asString = std::to_string(value);
return 0;
}
#include <string>
#include <sstream>
int main()
{
std::stringstream stream;
__int64 value(1000000000);
stream << value;
std::string strValue(stream.str());
return 0;
}
There are a few ways to do it:
strtoll(str, NULL, 10);
This is POSIX C99 compliant.
you can also use strtoimax; which has the following prototype:
strtoimax(const char *str, char **endptr, int base);
This is nice because it will always work with the local intmax_t ... This is C99 and you need to include <inttypes.h>
A C99 conforming attempt.
[edit] employed @R. correction
// Note: Typical values of SCNd64 include "lld" and "ld".
#include <inttypes.h>
#include <stdio.h>
int64_t S64(const char *s) {
int64_t i;
char c ;
int scanned = sscanf(s, "%" SCNd64 "%c", &i, &c);
if (scanned == 1) return i;
if (scanned > 1) {
// TBD about extra data found
return i;
}
// TBD failed to scan;
return 0;
}
int main(int argc, char *argv[]) {
if (argc > 1) {
int64_t i = S64(argv[1]);
printf("%" SCNd64 "\n", i);
}
return 0;
}
I've been reading some articles online why converting an int64 value to string using a combination of int() and strconv.Itoa isn't really a good idea but I am having issues trying to really grasp what it's all about. I understand there could be some inconsistencies during the conversion process but I am unable to grasp why such inconsistency could happen. I'll really appreciate it if some can explain to me like I am 5. A sample of what I am referring to can be seen below. Thanks
var amt int64 = 90 strconv.Itoa(int(amt))