π’ Convert int to string in Go
Β·1 min
Please disable your ad blocker π. Our ads are not intrusive
Table of Contents
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
inttostringyou can also usestrconv.Itoawhich is equivalent tostrconv.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)