OneBite.Dev - Coding blog in a bite size

convert variable from string to int in Go

Code snippet on how to convert variable from string to int in Go

  i, err := strconv.Atoi(s)

This line of code is used to convert a string value of s to an integer value of i in the programming language Go. The function strconv.Atoi() is used to convert string to integer type and the returned value stored in i and the error stored in err if any. Since the syntax of Go language does not allow direct conversion from string to integer type, this is the recommended way of doing so. In this code, the string s is passed as an argument to the function strconv.Atoi() and the function returns the converted integer in i and an error, if any, in err. If there is no error, err is equal to nil.

go