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.4k Golang : Command line ticker to show work in progress
+24.4k Golang : How to write CSV data to file
+37.8k Golang : How to iterate over a []string(array)
+21.3k Golang : Test file read write permission example
+6.2k Golang : How to stop user from directly running an executable file?
+8k Golang : How to check if a string with spaces in between is numeric?
+13.8k Golang : Force download file example
+9.6k Golang : Simple image viewer with Go-GTK
+7.8k Golang : How to get username from email address
+14.9k Golang : Get file permission
+11.9k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+7.5k Golang : Progress bar with ∎ character