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.2k Golang : Scramble and unscramble text message by randomly replacing words
+5.9k Golang : Normalize email to prevent multiple signups example
+3.8k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+25.7k Golang : Find files by name - cross platform example
+3.5k Java : Generate multiplication table example
+19.9k Golang : Convert string slice to struct and access with reflect example
+4.9k Golang : Generate multiplication table from an integer example
+12.4k Golang : Activate web camera and broadcast out base64 encoded images
+16.1k Golang : How to remove certain lines from a file
+13.5k Golang : Accurate and reliable decimal calculations
+3k Golang : PGX CopyFrom to insert rows into Postgres database
+9k Golang : Get escape characters \u form from unicode characters