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
+4.8k Grep : How to grep for strings inside binary data
+15.5k Golang : When to use init() function?
+26.1k Golang : How to verify uploaded file is image or allowed file types
+9.9k Golang : Change date format to yyyy-mm-dd
+5.7k Golang : Create zip/ePub file without compression(use Store algorithm)
+5.4k Web : How to see your website from different countries?
+8.9k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+9.7k Golang : Decompress zlib file example
+16.1k Golang : Read binary file into memory
+5.8k Javascript : Put image into Chrome browser's console
+4.9k Unix/Linux : How to get own IP address ?
+7.2k Yum Error: no such table: packages