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.
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.