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
+7.4k Linux : How to fix Brother HL-1110 printing blank page problem
+8.2k Golang : Check from web if Go application is running or not
+22.2k Golang : Match strings by wildcard patterns with filepath.Match() function
+9.7k Golang : Find correlation coefficient example
+20.2k Golang : Reset or rewind io.Reader or io.Writer
+14.3k Golang : Get uploaded file name or access uploaded files
+7.6k Golang : Detect sample rate, channels or latency with PortAudio
+34.1k Golang : Create x509 certificate, private and public keys
+25.1k Golang : Create PDF file from HTML file
+26.7k Golang : Encrypt and decrypt data with AES crypto
+12.4k Golang : How to check if a string starts or ends with certain characters or words?
+15.9k Golang : Read a file line by line