Golang strconv.ParseInt() function “converts the given string to an integer value in the provided base (0, 2 to 36) and bit size (0 to 64)”.
Syntax
func ParseInt(str string, base int, bitSize int) (i int64, err error)
Parameters
- str: String to be converted into an integer number.
- base: The base value of the given value. It can range from 0, 2, to 36.
- bitSize: It specifies the integer type, such as int(0), int8(8), int16(16), int32(32), and int64(64).
Return value
The strconv.ParseInt() function returns a pair of values: the converted integer and an error. If the conversion is successful, the error will be nil. If the conversion fails, the error will contain a description of the error.
Example 1
package main
import (
"fmt"
"strconv"
)
func main() {
str := "1921"
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(i)
}
Output
1921
Example 2
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.ParseInt("65535", 10, 32))
fmt.Println(strconv.ParseInt("+65535", 10, 32))
fmt.Println(strconv.ParseInt("-65535", 10, 32))
fmt.Println()
fmt.Println(strconv.ParseInt("101010", 2, 32))
fmt.Println(strconv.ParseInt("-111111", 2, 32))
fmt.Println(strconv.ParseInt("00001111000", 2, 32))
fmt.Println(strconv.ParseInt("-11111111111", 2, 64))
fmt.Println()
fmt.Println(strconv.ParseInt("65535", 16, 32))
fmt.Println(strconv.ParseInt("12ffcc", 16, 32))
fmt.Println(strconv.ParseInt("fafafa", 16, 64))
fmt.Println(strconv.ParseInt("ffffff", 16, 64))
str := "19KBKL21"
fmt.Println(strconv.ParseInt(str, 2, 32))
fmt.Println(strconv.ParseInt(str, 16, 64))
fmt.Println()
}
Output
65535 <nil>
65535 <nil>
-65535 <nil>
42 <nil>
-63 <nil>
120 <nil>
-2047 <nil>
415029 <nil>
1245132 <nil>
16448250 <nil>
16777215 <nil>
0 strconv.ParseInt: parsing "19KBKL21": invalid syntax
0 strconv.ParseInt: parsing "19KBKL21": invalid syntax
That’s it.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Distributed and cloud computing and is an expert in Go Language.