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.9k Yum Error: no such table: packages
+22.2k Golang : Use TLS version 1.2 and enforce server security configuration over client
+17.2k Golang : Covert map/slice/array to JSON or XML format
+29k Golang : Detect (OS) Operating System
+7.2k Web : How to see your website from different countries?
+11.3k Golang : Simple image viewer with Go-GTK
+5.5k Golang : How to deal with configuration data?
+7.1k Default cipher that OpenSSL used to encrypt a PEM file
+9.7k Golang : Extract or copy items from map based on value
+10.8k Golang : Allow Cross-Origin Resource Sharing request
+15.5k Golang : How to get Unix file descriptor for console and file