Golang convert int to string example
Posted on: February 07, 2021 by Deven
In Golang, you can convert int to string using the following functions.
- strconv.FormatInt
- strconv.Itoa
1. Golang convert int to string using strconv.Itoa
package main
import (
"fmt"
"strconv"
)
func main() {
convert := 9278
str1 := strconv.Itoa(convert)
fmt.Println(str1)
}
we converted the convert
variable into a string using Itoa
method. the above code ouputs 9278
2. Golang convert int to string using strconv.FormatInt
package main
import (
"fmt"
"strconv"
)
func main() {
convert2 := 1234
str2 := strconv.FormatInt(int64(convert2), 10)
fmt.Println(str2)
}
we converted the convert2
variable into a string using FormatInt
method. the above code outputs 1234
. check out Golang convert string to int – example article to do vice versa.
Share on social media
//