How to Convert a string to an int

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Converting a string to an integer is a common operation that is required in many programming tasks. If you’re looking to learn how to convert a string to an integer in Golang, you’re in the right place.

Before diving into the code examples, let’s first understand the theory behind converting a string to an integer in Golang.

In Golang, a string is a sequence of characters, and an integer is a numerical value. Converting a string to an integer involves extracting the numerical value from the string and converting it to an integer.

Golang provides two main methods for converting a string to an integer. The first method is by using the strconv.Atoi() function, which parses a string and returns the corresponding integer value. The second method is by using the strconv.ParseInt() function, which parses a string and returns an integer of a specific bit size.

Here’s an example of the first method:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "123"
    i, err := strconv.Atoi(s)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println(i)
}

In the code above, we declare a string variable called “s” with a value of “123”. We use the strconv.Atoi() function to parse the “s” variable and convert it to an integer, which we store in a variable called “i”. We then print the “i” variable using the fmt.Println() function.

Here’s an example of the second method:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    s := "123"
    i, err := strconv.ParseInt(s, 10, 64)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println(i)
}

In the code above, we declare a string variable called “s” with a value of “123”. We use the strconv.ParseInt() function to parse the “s” variable and convert it to an integer of 64 bits, which we store in a variable called “i”. We then print the “i” variable using the fmt.Println() function.

So why would someone want to convert a string to an integer in Golang? One common use case is when working with user input data that is in the form of a string. By converting the string to an integer, you can easily perform numerical operations on the data.

Another use case is when working with external APIs that return data in the form of a string. By converting the string to an integer, you can easily work with the data and perform numerical operations on it.

In conclusion, converting a string to an integer in Golang is a common operation that you’ll encounter frequently in your Golang programming journey. By using the strconv.Atoi() or strconv.ParseInt() function, you can easily extract the numerical value from a string and convert it to an integer. This can help you work with user input data and external APIs more efficiently.

I hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them below.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!