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
+5.7k Golang : How to setup a disk space used monitoring service with Telegram bot
+8.8k Golang : Identifying Golang HTTP client request
+41.1k Golang : Get hardware information such as disk, memory and CPU usage
+8k Golang : Populate or initialize struct with values example
+14.2k Golang : Validate hostname
+5.9k CloudFlare : Another way to get visitor's real IP address
+22.5k Golang : Use regular expression to validate domain name
+11.4k Golang : Remove or trim extra comma from CSV
+10.4k Golang : Fuzzy string search or approximate string matching example
+8.7k Golang : How to check if a website is served via HTTPS
+10.7k Golang : How to detect a server/machine network interface capabilities?
+8.3k Golang : Web(Javascript) to server-side websocket example