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.4k Golang : Convert(cast) []byte to io.Reader type
+21.6k Golang : Use TLS version 1.2 and enforce server security configuration over client
+7.1k Golang : How to detect if a sentence ends with a punctuation?
+6.3k Golang : Combine slices of complex numbers and operation example
+7.6k Golang : Regular Expression find string example
+78.5k Golang : How to return HTTP status code?
+7.1k Golang : Example of custom handler for Gorilla's Path usage.
+12.2k Elastic Search : Return all records (higher than default 10)
+5.5k Swift : Get substring with rangeOfString() function example
+6k Golang & Javascript : How to save cropped image to file on server
+6.6k Default cipher that OpenSSL used to encrypt a PEM file
+9.8k CodeIgniter : Load different view for mobile devices