Golang : convert int to string
While working on previous tutorial on displaying struct
values in string format with a method. I stumbled upon an age old question many programmers will eventually face one day. How to convert type integer to string.
In Go, it is pretty simple. Just use strconv.Itoa()
function to convert integer to string.
Here is the source code example :
package main
import (
"strconv"
"fmt"
)
func main() {
age := 12
// will NOT display properly
fmt.Println(string(age))
// convert int to string
agestr := strconv.Itoa(age)
// will display properly
fmt.Println(agestr)
}
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+20.2k Golang : How to get time zone and load different time zone?
+11.6k Golang : HTTP response JSON encoded data
+19.3k Nginx + FastCGI + Go Setup.
+6.8k Golang : Convert(cast) io.Reader type to string
+9.9k Golang : Command line file upload program to server example
+12.3k Golang : Convert(cast) int to int64
+5.5k Golang : Detect face in uploaded photo like GPlus
+11.1k Golang : md5 hash of a string
+8.5k Golang : Temperatures conversion example
+8.8k Golang : Eroding and dilating image with OpenCV example
+7.5k Swift : Convert (cast) Character to Integer?
+33.4k Golang : Converting a negative number to positive number