Let’s get started.
Int to String
Let us start with the most basic. How to convert an int to a string in go?
In go, we can convert an integer to a string using the help of a few functions from the strconv package.
The package comes with the FormatInt(), Itoa(), and Sprintf() functions, allowing you to convert an int type to string.
FormatInt()
The FormatInt() method allows you to convert an integer to its string representation in the given base where for 2 <= base <= 36.
Consider the example below:
import (
"fmt"
"reflect"
"strconv"
)
func main() {
var my_int int64 = 12374728291
to_str := strconv.FormatInt(my_int, 10)
fmt.Println(reflect.TypeOf(to_str))
}
The above example uses the FormatInt() function to perform the conversion of an integer (int64) to a string.
The resulting value of the typed string is shown from the reflect.TypeOf() method:
Itoa()
There is an even quicker and more efficient way to convert an int base 10 to a string. Using the Itoa() function, we can pass the target int value to convert. The function then returns the string representation of the specified value.
The Itoa() function is an equivalent of passing an int64 and the base of 10 to the FormatInt() method.
An example is as shown:
import (
"fmt"
"reflect"
"strconv"
)
func main() {
var my_int int64 = 100
to_str := strconv.Itoa(int(my_int))
fmt.Printf("%s is of type => %s ", to_str, reflect.TypeOf(to_str))
}
In this example, we use the Itoa() method to convert an integer type to a string.
Sprintf()
Another method you can use to convert an int to a string is to use the fmt.sprintf() method. The sprintf function takes the int and converts it to the specified format specifier, and returns the string format.
An example is as shown:
import (
"fmt"
"reflect"
)
func main() {
var my_int int = 100
to_str := fmt.Sprint(my_int)
fmt.Printf("%s is of type => %s ", to_str, reflect.TypeOf(to_str))
}
Similarly, we use the Sprintf() method to convert an int to a string, as shown in the output below:
100 is of type => string
String to Int
Another common type of conversion is to turn a string into an integer. Let us discuss the methods you can use to accomplish this in the go.
Atoi()
The first method we can use to convert a string to an int is the Atoi() method from the strconv package.
The function takes a string containing numerical values and converts it into an integer. Consider the example below:
import (
"fmt"
"reflect"
"strconv"
)
func main() {
str := "566"
my_int, _ := strconv.Atoi(str)
fmt.Println(reflect.TypeOf(my_int))
}
The above example takes the string value from the str variable and converts it to an int using the Atoi() or ASCII to Int method.
The Atoi method returns a similar result to the ParseInt(s, 10, 0) converted to type int.
ParseInt()
This is a nearly similar method that converts a string to an int type. It is part of the strconv package.
The function syntax is as shown:
It accepts the string, base as an integer, and the bit size as an int value.
An example use case is as shown:
import (
"fmt"
"reflect"
"strconv"
)
func main() {
str := "566"
my_int, _ := strconv.ParseInt(str, 10, 0)
fmt.Println(reflect.TypeOf(my_int))
}
In the example above, we convert the specified string to base-10 (decimal) with a bit size (size of the integer) of 0. This returns a 64-bit integer or int64.
You can also generate a 32-bit integer by setting the bit size to 32.
NOTE: The bitSize value of the ParseInt() method ranges from 0 – 64.
Completion
This guide covered the basics of performing string to int and int to string conversion in the go programming language.