Here are three ways to convert an integer to a string in Go.
- Using strconv.Itoa()
- Using strconv.FormatInt()
- Using strconv.Sprintf()
Method 1: Using Itoa() function
The “strconv.Itoa()” function returns the string representation of input when integer base 10 value.
Syntax
func Itoa(i int) string
Example
package main
import (
"fmt"
"strconv"
)
func main() {
data := 21
s := strconv.Itoa(data)
fmt.Printf("%T, %v\n", s, s)
}
Output
string, 21
Method 2: Using FormatInt() function
The strconv.FormatInt() function returns the string representation of i in the given base for 2 <= base <= 36. The result uses the lowercase letters ‘a’ to ‘z’ for digit values >= 10.
Syntax
func FormatInt(i int64, base int) string
Example
package main
import (
"fmt"
"strconv"
)
func main() {
data := int64(-19)
str10 := strconv.FormatInt(data, 10)
fmt.Printf("%T, %v\n", str10, str10)
str16 := strconv.FormatInt(data, 16)
fmt.Printf("%T, %v\n", str16, str16)
}
Output
string, -19
string, -13
Method 3: Using Sprintf() function
The fmt.Sprintf() function accepts the format and string and returns the formatted string.
Syntax
func Sprintf(format string, a ...interface{}) string
Example
package main
import (
"fmt"
"io"
"os"
)
func main() {
data := 19
str := fmt.Sprintf("%v \n", data)
io.WriteString(os.Stdout, str)
}
Output
19
Conclusion
For performance-sensitive applications, strconv.Itoa() and strconv.FormatInt() functions are generally faster and more efficient than fmt.Sprintf() function. This is because fmt.Sprintf() function parses the format string and is designed to handle various types and formatting options, which adds overhead.
In summary, if performance is a key concern and you’re doing straightforward integer-to-string conversions, you may want to stick with strconv.Itoa() or strconv.FormatInt().
Related posts

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.