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
+3k PHP : Extract part of a string starting from the middle
+3.8k Get website traffic ranking with Similar Web or Alexa
+10.9k Golang : Get digits from integer before and after given position example
+12.9k Golang : Capture stdout of a child process and act according to the result
+13k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+8.3k Golang : Surveillance with web camera and OpenCV
+24.7k Golang : missing Git command
+3.9k Golang : Find change in a combination of coins example
+8.6k Golang : Qt progress dialog example
+7.3k Golang : Find age or leap age from date of birth example
+25.8k Golang : How to split or chunking a file to smaller pieces?
+8k Golang : Simple File Server