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
+50.5k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+22.4k Golang : Round float to precision example
+13.6k Golang : How to check if a file is hidden?
+18.4k Unmarshal/Load CSV record into struct in Go
+18.9k Golang : Populate dropdown with html/template example
+48.2k Golang : Upload file from web browser to server
+14.2k Golang : On enumeration
+20.1k Android Studio : AlertDialog and EditText to get user string input example
+17.3k How to enable MariaDB/MySQL logs ?
+8.8k Golang : Serving HTTP and Websocket from different ports in a program example
+12.7k Python : Convert IPv6 address to decimal and back to IPv6
+5.6k Golang : Find change in a combination of coins example