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.1k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+13k Golang : List objects in AWS S3 bucket
+6.5k Golang : Totalize or add-up an array or slice example
+4.6k JavaScript : Rounding number to decimal formats to display currency
+11.6k Golang : How to detect a server/machine network interface capabilities?
+6.6k Golang : Reverse by word
+18.4k Golang : Aligning strings to right, left and center with fill example
+13.7k Golang : How to determine if a year is leap year?
+5.9k Fontello : How to load and use fonts?
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+9.4k Golang : Extract or copy items from map based on value
+5.9k Golang : Use NLP to get sentences for each paragraph example