Parse string to specific type of int (int8, int16, int32, int64)

As per documentation

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

ParseInt always return int64 no matter what. Moreover

The bitSize argument specifies the integer type that the result must
fit into

So basically the your bitSize parameter only tells that the string value that you are going to parse should fit the bitSize after parsing. If not, out of range will happen.

Like in this PlayGround: strconv.ParseInt("192", 10, 8) (as you see the value after the parsing would be bigger than maximum value of int8).

If you want to parse it to whatever value you need, just use int8(i) afterwards (int8, int16, int32).

P.S. because you touched the topic how to convert to specific intX, I would outline that it is also possible to convert to unsigned int.

Leave a Comment