Golang converting string to int64

I want to convert a string to an int64. What I find from the strconv package is the Atoi function. It seems to cast a string to an int and return it:

// Atoi is shorthand for ParseInt(s, 10, 0).
func Atoi(s string) (i int, err error) {
        i64, err := ParseInt(s, 10, 0)
    return int(i64), err

The ParseInt actually returns an int64:

func ParseInt(s string, base int, bitSize int) (i int64, err error){

So if I want to get an int64 from a string, should I avoid using Atoi, instead use ParseInt? Or is there an Atio64 hidden somewhere?

No, there’s no Atoi64. You should also pass in the 64 as the last parameter to ParseInt, or it might not produce the expected value on a 32-bit system.

Refer: stackoverflow

1 Like