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
+8.8k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+5.1k Python : Delay with time.sleep() function example
+7.8k Golang : Append and add item in slice
+13.2k Golang : Read XML elements data with xml.CharData example
+5.5k Fix yum-complete-transaction error
+11.9k Golang : List running EC2 instances and descriptions
+3k Golang : Fix go-cron set time not working issue
+22.4k Golang : simulate tail -f or read last line from log file example
+13.9k Golang : Convert IP version 6 address to integer or decimal number
+19.3k Golang : Close channel after ticker stopped example
+36.1k Golang : Convert(cast) int64 to string
+14.9k Golang : Get HTTP protocol version example