Cookies management by TermsFeed Cookie Consent

πŸ”’ 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)

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

🎠 Round float to any precision in Go

Learn how to create a universal rounding function
introduction numbers math

♾️ Infinite loop in Go

Learn how to define a “while true” loop
introduction loop

πŸ“” Convert a struct to io.Reader in Go

Learn how to convert a struct to io.Reader and send it as an HTTP POST request body
introduction http