Home » Golang Int to String Conversion

Golang Int to String Conversion

There are two ways to convert an int to a string in Golang:

  • Using the FormatInt() function
  • Using the Itoa() function

In this article, we will discuss how to convert int to string in golang using the FormatInt and Itoa() functions of the strconv package.

How to Convert Int to String in Golang Using FormatInt()

Use the FormatInt function of the strconv package in golang to convert int to string. The FormatInt() function takes two input parameters: the int to be converted and the base.

The base is the number system that the int should be converted to. For example, to convert the int to a decimal string, use the base of 10.

Refer to the below golang program that shows how to use the FormatInt function for converting int to string.

package main

import (
	"fmt"
	"strconv"
)

func main() {

	// Convert int64 to decimal string
	// use base = 10

	x := 14
	str1 := strconv.FormatInt(int64(x), 10)
	fmt.Printf("Int64 conversion to string is %v and its type is: %T\n", str1, str1)

	// Convert int64 to Hexadecimal string
	// use base = 16
	x = 14
	str1 = strconv.FormatInt(int64(x), 16)
	fmt.Printf("Int64 conversion to string is %v and its type is: %T\n", str1, str1)

	// Comvert int64 to Binary string
	// use base = 2

	x = 14
	str1 = strconv.FormatInt(int64(x), 2)
	fmt.Printf("Int64 conversion to string is %v and its type is: %T\n", str1, str1)

	// Convert int to Octal string
	// use base = 8
	x = 14
	str1 = strconv.FormatInt(int64(x), 8)
	fmt.Printf("Int64 conversion to string is %v and its type is: %T\n", str1, str1)
}

Output

Int64 conversion to string is 14 and its type is: string
Int64 conversion to string is e and its type is: string
Int64 conversion to string is 1110 and its type is: string
Int64 conversion to string is 16 and its type is: string

In the above Golang program, we imported the strconv package to use the FormatInt function.

Using the FormatInt function in golang, you can cast int to string using the different base as required.

For example, to convert the int to a hexadecimal string, use base = 16, refer to the code snippet

strconv.FormatInt(int64(x), 16)

It returns the string representation for the number. If the number is greater than 10, it uses the lowercase character ‘a’ to ‘z’ in the result.

Note: FormatInt function takes int64 type as input parameter, hence if you are trying to pass the int type, it will throw an error "cannot use x (variable of type int) as type int64 in argument to strconv.FormatInt". Hence use convert int to int64 using int64()

How to Convert Int to String in Golang Using Itoa()

The Itoa function of the strconv package is equivalent to FormatInt(int64(i), 10).

The Itoa function takes an integer number as an input parameter and returns the string representation of the number to the base 10.

The following golang code snippet shows how to use the Itoa function to convert int to string:

package main

import (
	"fmt"
	"strconv"
)

func main() {

	// Convert int64 to decimal string
	// use base = 10

	x := 14
	str1 := strconv.FormatInt(int64(x), 10)
	fmt.Printf("Int64 conversion to string is %v and its type is: %T\n", str1, str1)

	// Convert int to string
	// use Itoa function
	x = 14
	str1 = strconv.Itoa(x)
	fmt.Printf("Int conversion to string is %v and its type is: %T\n", str1, str1)
}

Output

Int64 conversion to string is 14 and its type is: string
Int conversion to string is 14 and its type is: string

Conclusion

I hope the above article helped you to understand how to convert int64 to string using the FormatInt() function and integer to string conversion using the Itoa() function.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Recommended Content

Golang Round float to the Integer value

Golang Round Float to Decimal Places

Golang Convert String to Integer

Leave a Comment