In this article you’ll learn about how you can convert or typecast a int to string In Golang, As each language have multiple ways to convert int to string Golang also supports multiple ways but all as per performance comparison some of Golang typecast methods are slow such as Sprintf() function of fmt package. If you try to run benchmark of all these at once you’ll see Sprintf() is taking more time than others, why because Sprintf() uses the reflect package and reflect package works on runtime which makes it slower than strconv.Itoa() and strconv.FormatInt() functions.
I’ll share the benchmark results in the end of the article but before that, below is the list of ways we are going to use to convert int to string in Golang:
- Using strconv.Itoa() function
- Using strconv.FormatInt() function
- Using fmt.Sprintf() function
Int to String Using Strconv.Itoa() function
The Itoa is a convenience function which convers an integer to a string.
func Itoa(i int) string
The Itoa is equivalent to FormatInt(int64(i), 10).
Below is the code of the following way to convert int to string in Golang:
// Program to convert int to string in Golang package main import ( "fmt" "strconv" ) func main() { // first way // using strconv.Itoa() s := 123 fmt.Printf("before converting %d is type of %T\n", s, s) i := strconv.Itoa(s) fmt.Printf("after converting %s is type of %T\n", i, i) }
In the code example we are converting s variable to int with the help of strconv.Itoa() function and then we’re also checking the type in fmt.Printf function.
If you run this code you’ll see the following output:
$go run .\main.go
before converting 123 is type of int
after converting 123 is type of string
Int to String Using strconv.FormatInt() function
FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a’ to ‘z’ for digit values >= 10.
func FormatInt(i int64, base int) string
Below is the code of the following way to convert int to string in Golang.
// Program to convert int to string in Golang package main import ( "fmt" "strconv" ) func main() { // second way // using strconv.FormatInt() s := 6542344 fmt.Printf("before converting %d is type of %T\n", s, s) i := strconv.FormatInt(int64(s), 10) fmt.Printf("after converting %s is type of %T\n", i, i) }
As you can see FormatInt accepts two parameters first is the int type 64 that’s why we convert our int to int64 with the help of int64 function, Second is the base which we passed 10.
Following is the output of the given code:
$go run .\main.go
before converting 6542344 is type of int
after converting 6542344 is type of string
Using fmt.Sprintf() function
Another way of converting an int to string is to use the fmt.Sprintf function. The function formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a …any) string
But this isn’t a good way of typecasting from integer to a string why you’ll get to know in the end of this article via Benchmark results.
Below is the code of the following approach in Golang:
// Program to convert int to string in Golang package main import ( "fmt" ) func main() { // third way // using fmt.Sprintf() s := 2547896 fmt.Printf("before converting %d is type of %T\n", s, s) i := fmt.Sprintf("%d", s) fmt.Printf("after converting %s is type of %T\n", i, i) }
As you can see we have passed %d formatter so fmt.Sprintf can recognize the type and do the conversion accordingly.
Below is the output of the following code:
$go run .\main.go
before converting 2547896 is type of int
after converting 2547896 is type of string
Now as you have seen all three ways, one thing comes in mind that which is the best and fastest way because in term of performance we need faster function to do typecasting, small things can cause big performance issues so below is the benchmark results of all approaches we used to convert int to string in Golang.
Below is the benchmark testing code stored in main_test.go file
package main import ( "fmt" "strconv" "testing" ) var smallInt = 40 var bigInt = 999999999999999 func Itoa(s int) string { i := strconv.Itoa(s) return i } func FormatInt(s int) string { i := strconv.FormatInt(int64(s), 10) return i } func Sprintf(s int) string { i := fmt.Sprintf("%d", s) return i } func BenchmarkItoa(b *testing.B) { for i := 0; i < b.N; i++ { Itoa(smallInt) } } func BenchmarkFormatInt(b *testing.B) { for i := 0; i < b.N; i++ { FormatInt(smallInt) } } func BenchmarkSprintf(b *testing.B) { for i := 0; i < b.N; i++ { Sprintf(smallInt) } } func BenchmarkItoaBig(b *testing.B) { for i := 0; i < b.N; i++ { Itoa(bigInt) } } func BenchmarkFormatIntBig(b *testing.B) { for i := 0; i < b.N; i++ { FormatInt(bigInt) } } func BenchmarkSprintfBig(b *testing.B) { for i := 0; i < b.N; i++ { Sprintf(bigInt) } }
Run the following command to get the benchmark results:
$go test -bench . -benchmem
As you can see above code’s benchmark results, according to benchmark results strconv.FormatInt is fast for small and big integers
I recommend you to try to do more benchmark testing on different inputs.
your comments are appreciated and if you wants to see your articles on this platform then please shoot a mail at this address kusingh@programmingeeksclub.com
Thanks for reading 🙂