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.
This article will cover the basics of converting strings to integers in Go, including how to do it safely and efficiently.
In Go, a string is a sequence of bytes that represent characters. When working with strings, we often need to convert them to other data types, such as integers. In this article, we will explore the different ways to convert a string to an integer in Go.
The Atoi
function is one of the easiest ways to convert a string to an integer in Go. It takes a single string argument and returns an integer value if the conversion was successful, or 0
if it wasn’t. Here’s an example:
str := "123"
num, err := strconv.Atoi(str)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Number:", num) // Output: 123
}
In this example, we first define a string variable str
with the value "123"
. We then use the Atoi
function to convert the string to an integer and store it in a new variable num
. If there was no error during the conversion, we print the resulting integer value.
The ParseInt
function is similar to the Atoi
function but allows you to specify the base of the number system. For example, if you want to convert a hexadecimal string to an integer, you can use the ParseInt
function with the base set to 16. Here’s an example:
str := "0xFF"
num, err := strconv.ParseInt(str, 16, 32)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Number:", num) // Output: 255
}
In this example, we first define a string variable str
with the value "0xFF"
. We then use the ParseInt
function to convert the hexadecimal string to an integer and store it in a new variable num
. If there was no error during the conversion, we print the resulting integer value.
The StringToInt
function is another way to convert a string to an integer in Go. It takes two arguments: the first is the string to be converted, and the second is the base of the number system. Here’s an example:
str := "123"
num := strconv.StringToInt(str, 10)
fmt.Println("Number:", num) // Output: 123
In this example, we first define a string variable str
with the value "123"
. We then use the StringToInt
function to convert the string to an integer and store it in a new variable num
. The base of the number system is set to 10. If there was no error during the conversion, we print the resulting integer value.
The StringToI64
function is similar to the StringToInt
function but allows you to convert a string to an integer of any size. Here’s an example:
str := "1234567890"
num := strconv.StringToI64(str, 10)
fmt.Println("Number:", num) // Output: 1234567890
In this example, we first define a string variable str
with the value "1234567890"
. We then use the StringToI64
function to convert the string to an integer of any size and store it in a new variable num
. The base of the number system is set to 10. If there was no error during the conversion, we print the resulting integer value.
The ParseUint
function is similar to the Atoi
and StringToInt
functions but allows you to convert a string to an unsigned integer. Here’s an example:
str := "123"
num, err := strconv.ParseUint(str, 10, 32)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Number:", num) // Output: 123
}
In this example, we first define a string variable str
with the value "123"
. We then use the ParseUint
function to convert the string to an unsigned integer and store it in a new variable num
. The base of the number system is set to 10. If there was no error during the conversion, we print the resulting unsigned integer value.
In conclusion, converting strings to integers in Go can be done using various functions such as Atoi
, ParseInt
, StringToInt
, and ParseUint
. Each function has its own specific use cases and advantages. By understanding these functions, you can choose the best one for your needs and write efficient and error-free code.