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
+13.3k Golang : Increment string example
+7.7k Golang : Ways to recover memory during run time.
+4.3k Golang : Valued expressions and functions example
+8.7k Yum Error: no such table: packages
+7.9k Golang : Check from web if Go application is running or not
+5.8k Golang : Shuffle array of list
+9.6k Golang : interface - when and where to use examples
+14.1k Golang : Simple word wrap or line breaking example
+4.8k Google : Block or disable caching of your website content
+9.3k Facebook : Getting the friends list with PHP return JSON format
+35.1k Golang : Strip slashes from string example
+16.3k CodeIgniter/PHP : Create directory if does not exist example