Converting a String to an Int in Go Image Converting a String to an Int in Go

In this code snippet, we are going to look at how you can convert a string variable to an int variable in Go using the strconv package.

In this example we define a string myAgeString which is equal to "26". In order to convert this string value to an int we will need to call strconv.Atoi() passing in the myAgeString. This will return the int value or an error which can can capture and handle.

We are then able to add 1 to this new int variable and then print it out using the %d format character.

Further Reading:

If you found this code snippet useful, you may also enjoy some of the other content on the site: