DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to convert a string type value to int type value in Go or Golang?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

How to convert a string type value to int type value in Go or Golang?

#go

Originally posted here!

To convert a string type value to an int type value, you can use the Atoi() method from the strconv standard package in Go or Golang.

The Atoi() method:

  • accepts a string type value as its argument
  • and returns 2 values, the first one being the converted int type value if any and the second being the error data if any.

TL;DR

package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "3000"
    // using the `Atoi()` method and passing
    // the value "3000" as an argument to it
    convertedIntvalue, err := strconv.Atoi("3000")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // log output of the `convertedIntvalue`
    // variable to the console
    fmt.Println(convertedIntvalue) // 3000 and the type is int βœ…
}
Enter fullscreen mode Exit fullscreen mode

For example, let's say we need to convert a string type value of "3000" to its corresponding int type value.

To do that let's first import the strconv standard package like this,

package main

// import `strconv` package
import "strconv"
Enter fullscreen mode Exit fullscreen mode

After importing the package, inside the main() function we can use the Atoi() method from the strconv package and pass the string type value of "3000" as an argument to the function.

It can be done like this,

package main

// import `strconv` package
import "strconv"

func main(){
    // convert `string` type value of "3000"
    // using the `Atoi()` method and passing
    // the value "3000" as an argument to it
    strconv.Atoi("3000")
}
Enter fullscreen mode Exit fullscreen mode

The Atoi() method returns 2 values where the first value is the converted int type value if successfully converted and the second value is the error data if anything happened during the conversion. To capture these 2 data, let's assign 2 variables.

It can be done like this,

package main

// import `strconv` package
import "strconv"

func main(){
    // convert `string` type value of "3000"
    // using the `Atoi()` method and passing
    // the value "3000" as an argument to it
    convertedIntvalue, err := strconv.Atoi("3000")
}
Enter fullscreen mode Exit fullscreen mode

Before dealing with the converted int value we need to check if there was any error during the conversion process. Let's use a simple if conditional statement and check to see if the err data variable is nil or not. If it is not nil, then there must have been an error that happened during the conversion.

It can be done like this,

package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main(){
    // convert `string` type value of "3000"
    // using the `Atoi()` method and passing
    // the value "3000" as an argument to it
    convertedIntvalue, err := strconv.Atoi("3000")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }
}
Enter fullscreen mode Exit fullscreen mode

If there is no error then it is safe to assume that the convertedIntvalue variable must be having the converted int type value. Let's log the output to the console like this,

package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "3000"
    // using the `Atoi()` method and passing
    // the value "3000" as an argument to it
    convertedIntvalue, err := strconv.Atoi("3000")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    // log output of the `convertedIntvalue`
    // variable to the console
    fmt.Println(convertedIntvalue) // 3000 and the type is int βœ…
}
Enter fullscreen mode Exit fullscreen mode

As you can see that the string type value of "3000" is successfully converted to its corresponding int type value which is what we want.

See the above code live in The Go Playground.

Now let's try to give a string type value composed of alphabets like John Doe and then try to convert it to its int type value like this,

package main

// import `strconv` package
import (
    "fmt"
    "strconv"
)

func main() {
    // convert `string` type value of "John Doe"
    // using the `Atoi()` method and passing
    // the value "John Doe" as an argument to it
    convertedIntvalue, err := strconv.Atoi("John Doe")
    // check if any error happened
    if err != nil {
        fmt.Println(err.Error()) // Error ❌. strconv.Atoi: parsing "John Doe": invalid syntax
        return
    }

    // log output of the `convertedIntvalue`
    // variable to the console
    fmt.Println(convertedIntvalue)
}
Enter fullscreen mode Exit fullscreen mode

As you can see that the Go compiler shows us an error saying that strconv.Atoi: parsing "John Doe": invalid syntax which essentially means that it cannot parse this value to its corresponding int type value.

See the above code live in The Go Playground.

That's all πŸ˜ƒ!

Feel free to share if you found this useful πŸ˜ƒ.


Top comments (0)

πŸ‘‹ Hey, my name is Noah and I’m the one who set up this ad. My job is to get you to join DEV, so if you fancy doing me a favor, I’d love for you to create an account.

If you found DEV from searching around, here are a couple of our most popular articles on DEV: