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.1k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+7.9k Golang : Check if integer is power of four example
+12k Elastic Search : Return all records (higher than default 10)
+23k Find and replace a character in a string in Go
+12.1k Golang : HTTP response JSON encoded data
+19.7k Golang : Count number of digits from given integer value
+5.1k Python : Delay with time.sleep() function example
+5.1k Golang : fmt.Println prints out empty data from struct
+6.5k Swift : substringWithRange() function example
+40.4k Golang : How to count duplicate items in slice/array?
+16k Golang : Delete files by extension