There are several ways for Golang int to string conversion. We will briefly discuss few of them in today’s tutorial. We have built in strconv
package to help us deal with the various data types. We can take help of that package to do our Int to String conversion task in the Go programming language.
strconv Package
The strconv
package in Go is part of the standard library and provides functions to convert strings to and from various basic data types, such as integers, floats, and booleans. It’s designed to be efficient and easy to use, making it an essential tool for developers working with text-based data.
Golang int to string using strconv.Itoa
One of the most simplest way to convert an integer to string is to use the `strconv.Itoa` function. This is very similar to the C `itoa` function. You can say this a Golang alternative to the `itoa` method of C language. Here is the quick code example to convert the integer to the string with the `strconv.Itoa` method.
package main
import (
"fmt"
"strconv"
)
func main() {
a := 1234
str := strconv.Itoa(a)
fmt.Println(str) // Output: "1234"
}
Code language: Go (go)
The strconv.Itoa function converts an integer to its string representation. One of the limitations of using Itoa
function is that it always converts the int to base 10 strings.
Using strconv.FormatInt
:
If we require to convert the Int to string other then the base 10 we can use the FormatInt
from the strconv
package as well. strconv.FormatInt
allows you to specify the base for the conversion, making it more versatile. It can also handle the int64 while the Itoa
cannot.
Here is the quick example of using strconv.FormatInt
for Int to string conversion in go language.
package main
import (
"fmt"
"strconv"
)
func main() {
a := int64(1234)
str := strconv.FormatInt(a, 10)
fmt.Println(str) // Output: "1234"
}
Code language: Go (go)
Here the first argument to the FormatInt
function is the integer which we want to convert to string. The second parameter is the base parameter.
The base parameter in the strconv.FormatInt
function specifies the numerical base (or radix) for the conversion of an integer to its string representation. This parameter allows you to convert the integer into different bases, ranging from binary (base 2) to hexadecimal (base 16), and even up to base 36. Here is the quick code example to convert the Int to base 2 or binary string in the golang.
package main
import (
"fmt"
"strconv"
)
func main() {
a := int64(10)
str := strconv.FormatInt(a, 2)
fmt.Println(str) // Output: "1010"
}
Code language: Go (go)
And, similarly to convert and Integer to Octal String you can change the base parameter to 8 like in following code.
package main
import (
"fmt"
"strconv"
)
func main() {
a := int64(10)
str := strconv.FormatInt(a, 8)
fmt.Println(str) // Output: "12"
}
Code language: CSS (css)
Even the Base-16 and the Base-32 string could be generated with this function using digits 0-9 and letters a-z as follows.
package main
import (
"fmt"
"strconv"
)
func main() {
a := int64(123456789)
str := strconv.FormatInt(a, 36)
fmt.Println(str) // Output: "21i3v9"
}
Code language: CSS (css)
Golang int to string using fmt.Sprintf
:
Just like the C style of formating, we have the famous sprintf
function available in the standard fmt
package. So, if you are comfortable with the C style sprintf
fomating, you can use the fmt.sprintf
in golang as well. To use the sprintf
in golang to convert Integer to string you can use the following code.
package main
import (
"fmt"
)
func main() {
a := 1234
str := fmt.Sprintf("%d", a)
fmt.Println(str) // Output: "1234"
}
Code language: Go (go)
The Sprintf function formats the integer as a string according to a format specifier.
These methods should cover most use cases for converting integers to strings in Go. Which one do you think you’ll use?