How to Convert String to Int in Go

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.


Learn how to convert strings to integers in Go, including the different methods available and how to handle errors.



In Go, a string is a sequence of characters that can be represented as a slice of bytes. However, when working with numeric data, it’s often necessary to convert strings to integers. This article will cover the different ways to convert strings to int in Go, including the use of built-in functions and the importance of error handling.

Built-in Functions for String Conversion

Go provides several built-in functions that can be used to convert strings to integers:

  1. strconv.Atoi: This function converts a string representation of an integer into its corresponding int value. It returns an error if the input string is not a valid integer or if the integer value is outside the range of the int type.
  2. strconv.ParseInt: This function converts a string representation of an integer to its corresponding int64 value. It also takes a second argument that specifies the base (or radix) of the input string, which can be either 10 or 16. If the input string is not a valid integer, it returns an error.
  3. strconv.ParseUint: This function converts a string representation of an unsigned integer to its corresponding uint value. It also takes a second argument that specifies the base (or radix) of the input string, which can be either 10 or 16. If the input string is not a valid integer, it returns an error.
  4. strconv.ParseFloat: This function converts a string representation of a floating-point number to its corresponding float64 value. It also takes a second argument that specifies the base (or radix) of the input string, which can be either 10 or 16. If the input string is not a valid floating-point number, it returns an error.

Examples

Here are some examples of how to use these built-in functions to convert strings to integers:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	// Example 1: Atoi
	s := "42"
	i, err := strconv.Atoi(s)
	if err != nil {
		fmt.Println("Error converting string to int:", err)
	} else {
		fmt.Printf("The integer value of %q is %d\n", s, i)
	}

	// Example 2: ParseInt
	s = "0x19" // base 16
	i, err = strconv.ParseInt(s, 16, 32)
	if err != nil {
		fmt.Println("Error converting hex string to int:", err)
	} else {
		fmt.Printf("The integer value of %q is %d\n", s, i)
	}

	// Example 3: ParseUint
	s = "255" // base 10
	i, err = strconv.ParseUint(s, 10, 8)
	if err != nil {
		fmt.Println("Error converting string to uint:", err)
	} else {
		fmt.Printf("The unsigned integer value of %q is %d\n", s, i)
	}

	// Example 4: ParseFloat
	s = "3.14" // base 10
	i, err := strconv.ParseFloat(s, 64)
	if err != nil {
		fmt.Println("Error converting string to float:", err)
	} else {
		fmt.Printf("The floating-point value of %q is %f\n", s, i)
	}
}

Handling Errors

When working with strings that may not represent valid integers or floating-point numbers, it’s important to handle errors appropriately. The built-in functions in Go provide a way to return an error value if the conversion fails, which can be checked using a type assertion. Here is an example of how to handle errors:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	s := "not an integer"
	i, err := strconv.Atoi(s)
	if err != nil {
		fmt.Println("Error converting string to int:", err)
	} else {
		fmt.Printf("The integer value of %q is %d\n", s, i)
	}
}

In this example, the input string “not an integer” is not a valid integer, so the Atoi function returns an error. The error is checked using a type assertion and printed to the console if it is not nil.

Conclusion

Converting strings to integers in Go can be done using several built-in functions provided by the strconv package. These functions are useful for working with numeric data, but it’s important to handle errors appropriately to avoid unexpected behavior. By understanding how to convert strings to integers in Go, you can work more effectively with numeric data and build robust applications.



Stay up to date on the latest in Coding for AI and Data Science

Intuit Mailchimp