Convert int/int64 to string

yourbasic.org/golang

int to string

Use the strconv.Itoa function to convert an int to a decimal string.

s := strconv.Itoa(109) // s == "109"

int64 to string

Use strconv.FormatInt to format an integer in a given base.

var n int64 = 109
s := strconv.FormatInt(n, 10) // decimal
fmt.Println(s)                // 109
var n int64 = 109
s = strconv.FormatInt(n, 16) // hexadecimal
fmt.Println(s)                // 6d

General formatting

The fmt.Sprintf function is a useful general tool for converting data to string:

s := fmt.Sprintf("%4d", 109) // s == " 109" (width 4, right justify)

Further reading

The fmt cheat sheet covers the most common formatting flags.

Share this page: