Golang String to Int: How to Convert Strings to Integers in Go

4 min read

In Go, converting a string to int can be done using the default strconv package. This package provides several functions for parsing strings into different numeric types, including integers. In this blog post, we will explore how to convert a Golang string to int using the strconv.Atoi() function.

The strconv.Atoi() function takes a string as an argument and returns an integer and an error. The integer value represents the converted string, while the error value indicates whether the conversion was successful or not.

package main import ( "fmt" "strconv" ) func main() { numAsString := "100" numAsInt, err := strconv.Atoi(numAsString) if err != nil { fmt.Println(err) return } fmt.Println(numAsInt) } // output: 100

Have a play in the Go Playground.

In the above code, we first declare a string variable numAsString with the value "100". We then use the strconv.Atoi() function to convert this string to an integer. The returned integer is assigned to the numAsInt variable, while the error (if any) is assigned to the err variable. Finally, we print the value of numAsInt, and just like that we successfully managed to do a golang string to int conversion and print its value to the console.

strconv.Atoi() is that it only works with integers in base 10. If you have a string representing an integer in a different base, you will need to use a different function from the strconv package. For example, strconv.ParseInt() can be used to parse integers in different bases.One thing to note about

Converting back from int to string

To convert back, we can simply make use of the strconv.Itoa() function which takes an integer as an argument and returns a string. The string value represents the converted integer.

package main import ( "fmt" "strconv" ) func main() { numAsInt := 100 numAsString := strconv.Itoa(numAsInt) fmt.Println(numAsString) } // output: 100

Have a play in the Go Playground.

In the above code, we first declare an integer variable numAsInt with the value 100. We then use the strconv.Itoa() function to convert this integer to a string. The returned string is assigned to the numAsString variable. Finally, we print the value of numAsVariable to the console.

One Go "gotcha" to take note of is that the strconv.Itoa() function does not handle errors. If you pass an invalid integer to the function, it will simply return the string "0". To handle errors, you should use the strconv.FormatInt() function, which returns an error if the integer is invalid.

Parsing an int with base and bit size

In addition to strconv.Itoa(), the strconv package in Go also provides the strconv.ParseInt() function for converting a string to an integer. This function takes three arguments:

  • The string to be converted.
  • The base of the integer in decimal notation, with base 10 being the most common way of representing integers in human-readable form.
  • The bit size of the integer we want returned up to 64.

As an example, strconv.ParseInt(numberString, 10, 32) is telling the strconv package to parse the string s as numberString integer, and return a 32-bit signed integer that can represent values within the range of -2147483648 to 2147483647. If the string cannot be parsed as an integer, an error will be returned.

In summary, it's important to understand how golang string to int conversions work, but also the "gotchas" and nuances which are involved in the process. Thanks to the the `strconv` package and its ParseInt(), Atoi() and Itoa() functions, converting strings to integers is a straightforward process that can greatly simplify your code as you start to work in golang.