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
+20.4k Golang : Saving private and public key to files
+7.1k Golang : Scanf function weird error in Windows
+6k Apt-get to install and uninstall Golang
+9.5k Golang : ffmpeg with os/exec.Command() returns non-zero status
+9.9k Golang : Print how to use flag for your application example
+14.5k Golang : Normalize unicode strings for comparison purpose
+29.4k Golang : Get time.Duration in year, month, week or day
+12.3k Golang : Add ASCII art to command line application launching process
+13.2k Golang : Get constant name from value
+12.3k Android Studio : Highlight ImageButton when pressed on example
+12.9k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+14.6k Golang : Submit web forms without browser by http.PostForm example