Convert int
to string
in GoLang
The simplest way to convert an int to string in Go is to use the fmt.Sprint
function. This will also work seamlessly for int64
types.
package main
import "fmt"
func main() {
myInt := 10
myStr := fmt.Sprint(myInt)
fmt.Println("My int: ", myInt)
fmt.Println("My string: ", myStr)
}
Alternatives to convert int
to string
Go also ships with a package called strconv
that can be used to convert an int
(not int64
) to a string:
package main
import (
"fmt"
"strconv"
)
func main() {
myInt := 10
myStr := strconv.Itoa(myInt)
fmt.Println("My int: ", myInt)
fmt.Println("My string: ", myStr)
}
Conclusion
Go has a number of easy builtin functions that can be used for converting (and formatting) int
data to string
s.