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
+4.7k JavaScript : Rounding number to decimal formats to display currency
+5.5k Golang : Reclaim memory occupied by make() example
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+5.3k Golang : The Tao of importing package
+4.5k Golang : Valued expressions and functions example
+11.8k Golang : Concurrency and goroutine example
+16.2k Golang : How to check if input from os.Args is integer?
+12.5k Elastic Search : Return all records (higher than default 10)
+9.7k Golang : How to generate Code 39 barcode?
+7k Default cipher that OpenSSL used to encrypt a PEM file
+19.3k Golang : Delete item from slice based on index/key position