</bugz loading .

3 ways to convert int to string [go]

Intro

Converting integers to strings is a common operation in programming allowing us to display numerical values as text. Go language provides several very easy ways to convert an int to string. Infact Go provides a separate package ( strconv ) to convert a string to various datatypes and vice versa.
In this blog, we will explore 3 of the simplest ways to convert an int to string in go.

1. Convert int to string using strconv.Itoa()

func Itoa(i int) string

The most common and the easiest way to convert an integer to string in golang is to use the Itoa() function of the strconv package. The strconv.Itoa() function takes an integer as argument and returns a string value in return. For example, strconv.Itoa() function takes 3 as argument and return '3' as srting in return.

package main

import (
	"fmt"
	"strconv" // importing strconv package
)

func main() {
	str := strconv.Itoa(69) // converting int to string
	fmt.Println(str)
	fmt.Printf("type : %T\n", str) // printing the type
}
69
type : string

2. Convert int to string using fmt.Sprintf()

func Sprintf(format string, a ...any) string

An alternative for the strconv.Itoa() function is the fmt.Sprintf() fucntion. It is a part of the fmt package. It offers a versatile way to format strings in Go. It supports a wide range of format specifiers, including %d for integers, allowing you to convert integers to strings with ease. Here is an example of converting a decimal int to string using it.

package main

import (
	"fmt" // importing fmt package
)

func main() {
	i := 42
	str := fmt.Sprintf("%d", i) // converting int to string
	fmt.Println(str)
	fmt.Printf("type : %T\n", str) // printing the type
}
69
type : string

3. Convert int to string using strconv.FormatInt()

func FormatInt(i int64, base int) string

strconv.FormatInt() function is similar to strconv.Itoa(). It provides better flexibility by allowing you to specify the base of the string representation of the integer. It is helpful when we want to convert an int to string in different numeric systems such as binary, hexadecimal, etc. This function takes a int64 and the base of the integer as arguments. The int64() function can be used to convert an int to int64.
Here is an example on converting int to string using strconv.FormatInt() :

package main

import (
	"fmt"
	"strconv" // importing strconv package
)

func main() {
	i := int64(42) // converting int to int64

	str := strconv.FormatInt(i, 10)   // decimal - string
	str_b := strconv.FormatInt(i, 2)  // binary - string
	str_o := strconv.FormatInt(i, 8)  // octal - string
	str_h := strconv.FormatInt(i, 16) // hexadecimal - string

	fmt.Printf("%s is of type : %T\n", str, str)
	fmt.Printf("%s is of type : %T\n", str_b, str_b)
	fmt.Printf("%s is of type : %T\n", str_o, str_o)
	fmt.Printf("%s is of type : %T\n", str_h, str_h)
}
42 is of type : string
101010 is of type : string
52 is of type : string
2a is of type : string

In the above example we are using strconv.FormatInt() funciton to convert an integer to string in various numerical systems ( decimal, binary, octal, hexadecimal ). This function first converts the given number to the integer of the specified base and then converts it into a string.

Conclusion

In conclusion, Go provides multiple functions to convert an integer to string each of which can be used in different scenarios depending upon the need.

  • 👉

    If a basic conversion is required strconv.Itoa() can be used

  • 👉

    strconv.FormatInt() can be used for more flexibility with bases

  • 👉

    fmt.Sprintf() can be used for versatile formatting.

We can choose the right function based on our requirements and efficiently convert integers to strings.