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
+7.9k Golang : Take screen shot of browser with JQuery example
+11.2k Golang : Search and extract certain XML data example
+13.9k Golang : Find location by IP address and display with Google Map
+8.1k Golang : How to check if a string with spaces in between is numeric?
+5.1k Golang : Get missing location after unmarshal binary and gob decode time.
+26.8k Golang : Read, Write(Create) and Delete Cookie example
+8.6k Golang : Turn string or text file into slice example
+6k Golang : Null and nil value
+16.1k Golang : When to use init() function?
+4.5k Swift : Convert string array to array example
+9.3k Golang : Generate random integer or float number
+10.3k Golang : Display a text file line by line with line number example