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
+6.2k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+15.5k Golang : Gzip file example
+5.2k Golang : Dealing with backquote
+9k Golang : Print how to use flag for your application example
+3.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+3.9k Golang : Calculate a pip value and distance to target profit example
+4.8k Javascript : How to refresh page with JQuery ?
+6.5k Golang : Handling Yes No Quit query input
+13.4k Golang : How to shuffle elements in array or slice?
+19.4k Golang : Convert date string to variants of time.Time type examples
+6.4k Golang : Scanf function weird error in Windows
+7.9k Golang : Take screen shot of browser with JQuery example