Development

How to convert a string to int in Golang

In this tutorial, you will come across two techniques you can use to convert a string type into an integer in the Go programming language.
Captain Salem 3 min read
How to convert a string to int in Golang

In Go, a string refers to a read-only slice of bytes. In simple terms, it refers to a sequence of characters of varying length. Each character is the string is represented as byte using UTF-8 encoding.

Due to the UTF-8 encoding, strings in Go can support a wide range of characters and symbols from a variety of languages.

Therefore, get your IDE ready, a cup of coffee and a curious mind and let's dive in.

Method 1 - Using Atoi() Function

The first and most common method we can use to convert a string to an int in Go is the Atoi function. This function is provided by the strconv package. Hence, we need to import it before conversion.

Function Syntax

The function syntax is as shown:

func Atoi(s string) (int, error)

The function takes on the string you wish to convert as the parameter and returns it as a int type.

Example

Let us take a simple example as shown in the code snippet below:

package main

// import the required packages
import (
	"fmt"
	"log"
	"reflect"
	"strconv"
)

// main function
func main() {
	my_str := "100"
	int_var, err := strconv.Atoi(my_str)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(int_var, reflect.TypeOf(int_var))

}

Let us break down the code above to understand how it works.

We start by importing the packages we wish to use in the program. For this example, we need the fmt, log, reflect , and strconv packages.

Next, in the main function, we create a variable called my_str holding the string we wish to convert to an int.

  • In the second line, we create a variable called int_var which holds the converted integer. We then call the Atoi function and pass the my_str as the variable.
  • Next, we check if the function encountered any errors and log them.
  • Finally, we print the returned integer value and its type using the TypeOf function.

We can then run the code as:

$ go run my.go 

The command above should return an output as:

100 int

Example 2 - Incorrect String Parsing

Keep in mind that the function will only convert the string to an integer if the string contains numeric characters only. If the string contains non-numeric characters, it'll return an error as shown:

package main

// import the required packages
import (
	"fmt"
	"log"
	"reflect"
	"strconv"
)

// main function
func main() {
	my_str := "hi"
	int_var, err := strconv.Atoi(my_str)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(int_var, reflect.TypeOf(int_var))

}

In this case, the value of my_str is non-numeric. Running the code above returns an error as shown:

2022/08/22 08:51:16 strconv.Atoi: parsing "hi": invalid syntax
exit status 1

Method 2 - Using the ParseInt() Function

The second method you can use when converting a string to an int is the ParseInt() function. This function interprets the string into a specified base from 0, 2 to 36 and specified bit size from 0 to 64. The function will then return the corresponding value as an integer,

Function Syntax

The function syntax is as shown:

func ParseInt(s string, base int, bitSize int) (i int64, err error)

In this case, the function takes the string to convert, the base, and the bit size.

Example

The example code below illustrates how to convert a string to int using the ParseInt() function.

package main

import (
	"fmt"
	"log"
	"reflect"
	"strconv"
)
func main() {
	str := "100"
	int_s, err := strconv.ParseInt(str, 0, 8)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(int_s, reflect.TypeOf(int_s))
}

In this case, we convert the specified string to an int of base 0 and 8 bits.

The resulting value is as shown:

Output:
100 int64

Conclusion

In this post, you discovered two methods you can use to convert a string to an int type in Golang. We hope you enjoyed this article.

Thanks for reading && Stay tuned for more!!!

If you enjoy our content, please consider buying us a coffee to support our work:

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.