🔢 Convert int to string in Go

introduction numbers integer

Use strconv.FormatInt function to convert an integer variable to a string in Go.

int64 to a decimal string

var number int64 = 12
str := strconv.FormatInt(number, 10)
fmt.Println(str)

int, int32, int16, int8 to a decimal string

var number int = 12 // you can use any integer here: int32, int16, int8
str := strconv.FormatInt(int64(number), 10)
fmt.Println(str)

To convert int to string you can also use strconv.Itoa which is equivalent to strconv.FormatInt(int64(i), 10).

number := 12
str := strconv.Itoa(number)
fmt.Println(str)

int64 to a hexadecimal string

var number int64 = 12 // you can use any integer here: int, int32, int16, int8
str := strconv.FormatInt(number, 16)
fmt.Println(str)

int64 to an octal string

var number int64 = 12 // int, int32, int16, int8
str := strconv.FormatInt(number, 8)
fmt.Println(str)

int64 to a binary string

var number int64 = 12 // int, int32, int16, int8
str := strconv.FormatInt(number, 2)
fmt.Println(str)

🖨️ Convert string to []byte or []byte to string in Go

Learn the difference between a string and a byte slice
introduction strings slice

🧠 Print the memory address of a variable in Go

Learn how to find and print the address of a variable or pointer
introduction pointer slice

🏟️ Capacity and length of a slice in Go

Learn what is the difference between length and capacity of a slice
introduction slice array