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
+9.2k Golang : How to profile or log time spend on execution?
+25.7k Golang : Find files by name - cross platform example
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+5.6k Elasticsearch : Shutdown a local node
+21.2k Generate checksum for a file in Go
+4.8k Golang : Struct field tags and what is their purpose?
+15.5k Golang : How to generate QR codes?
+16.2k Golang : Parse date string and convert to dd-mm-yyyy format
+4.2k PHP : See installed compiled-in-modules
+25.3k Golang : Force your program to run with root permissions
+26.6k Golang : Change a file last modified date and time