strconv.parseInt() never returns int

Greetings!

Question about string to int conversion using package: strconv

First, strconv.Atoi() returns type int.
Second, it appears that strconv.ParseInt() never returns a type int. However, the language definition says:

“The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.”

So, if I do:

n, _ := strconv.ParseInt("42", 10, 0)

n should be an int, not int64. Right? But, it is int64.

Before someone tells me “how to get a 32/64 bit int”: I am quoting the language spec and the compiler does not conform to the description quoted above.

Any ideas?

Thanks,

Deepak.

Golang does not have generic types or generic functions - there is no generic ParseInt (which would have different return types).

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0 , 8, 16, 32, and 64 correspond to int , int8, int16, int32, and int64.

This quotation does not say that the function returns int8, int16, int32, …; it just says the the result fits the bitSize. And the only integer that fits all of these is int64.

The function signature is:

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

It is fixed to int64 because this can fit all the other integers.

3 Likes

Yes!!! Makes sense now!

Thank you.

Deepak.

I’m glad I could help! :slightly_smiling_face:
To mark the topic as solved/closed you can accept the answer.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.