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
+15.4k Golang : Get current time from the Internet time server(ntp) example
+5.6k Golang : Find change in a combination of coins example
+11.9k Golang : md5 hash of a string
+27.2k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+19.2k Golang : How to Set or Add Header http.ResponseWriter?
+11k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+6.3k PHP : Shuffle to display different content or advertisement
+30.1k Golang : How to redirect to new page with net/http?
+5.5k Golang : Struct field tags and what is their purpose?
+8.8k Golang : Get curl -I or head data from URL example
+20.9k Golang : Convert(cast) string to rune and back to string example
+9.4k Random number generation with crypto/rand in Go