strconv.ParseInt() FunctionThe strconv packageās ParseInt() function converts the given string s to an integer value i in the provided base (0, 2 to 36) and bit size (0 to 64).
Note: To convert as a positive or negative integer, the given
stringmust begin with a leading sign, such as+or-.
func ParseInt(s string, base int, bitSize int) (i int64, err error)
s: String value to be converted in 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).
The returned value is int, error type.
The strconv.ParseInt() function returns the integer number converted from the given string.
The following code shows how to implement the strconv.ParseInt() function in Golang:
// Golang program// strconv.ParseInt() Functionpackage mainimport ("fmt""strconv")func main() {// delare variable of type stringvar str string// assigning valuestr = "1041"// ParseInt() converts variable str to a integer number// with the specified base and bitSize// diplays resultfmt.Println(strconv.ParseInt(str, 10, 32))fmt.Println(strconv.ParseInt(str, 2, 64))fmt.Println()// reassigning valuestr = "-1041"// ParseFloat() converts variable str to a integer number// with the specified base and bitSize// diplays resultfmt.Println(strconv.ParseInt(str, 8, 32))fmt.Println(strconv.ParseInt(str, 16, 64))fmt.Println()// reassigning valuestr = "1Shots12"// ParseInt() converts variable str to a integer number// with the specified base and bitSize// diplays resultfmt.Println(strconv.ParseInt(str, 2, 32))fmt.Println(strconv.ParseInt(str, 16, 64))fmt.Println()// reassigning valuestr = "00101110111111"// ParseFloat() converts variable str to a integer number// with the specified base and bitSize// diplays resultfmt.Println(strconv.ParseInt(str, 16, 32)) // throw out of range errorfmt.Println(strconv.ParseInt(str, 16, 64))fmt.Println()}
Line 1: We add the main package.
Lines 3 to 6: We import necessary packages in Golang project.
Lines 11 to 21: We define the main() function, variable str of string type and assign a value to it, and then converts variable str to an integer number using the ParseInt() function with the specified base and bitSize.
Finally, the result is displayed.