How to Convert Golang Int to String

Go provides numerous different types to represent the numbers. When we speak of numbers, we divide the numbers into two different types:

  1. integers
  2. floating-point numbers.

Golang integer has two types.

  1. signed integer: int8, int16, int32, int54
  2. unsigned integer: uint8, uint16, uint32, uint64

Golang string is a sequence of variable-width characters where each character is defined by one or more bytes using UTF-8 Encoding. First, let’s see how to convert an integer to a string using different functions.

Golang int to string

To convert int to string in Golang,

  1. strconv.Itoa(): The strconv.Itoa() function converts an integer base 10 value to ASCII string.
  2. strconv.FormatInt(): The strconv.Format() function converts int64 value to string.
  3. fmt.Sprintf(): The fmt.Sprintf() function formats according to a format specifier and returns the resulting string.

Using Itoa() function

Golang provides a strconv package that executes conversions to and from string representations of basic data types. For example, the Itoa() method belongs to the strconv package that converts an integer base 10 value to an ASCII string.

Syntax

func Itoa(i int) string

Example

package main

import (
 "fmt"
 "strconv"
)

func main() {
 data := 21
 s := strconv.Itoa(data)
 fmt.Printf("%T, %v\n", s, s)
}

Output

go run app.go

string, 21

In this example, we imported two built-in packages for Go.

  1. fmt
  2. strconv

The fmt package executes formatted I/O with functions similar to C’s printf() and scanf().

The strconv package implements conversions to and from basic data types string representations.

Inside the main() function, we defined a variable called data and assigned an integer value, and the data variable became an integer. Then we converted an integer to a string using Itoa() function, which accepts an integer and returns the string.

Using FormatInt() to convert Go int to string

To convert int to string in Golang, use the strconv.FormatInt() function. The FormatInt() is a function of the strconv package that converts an integer value to a string.

The FormatInt() function returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters ‘a’ to ‘z’ for digit values >= 10.

Syntax

func FormatInt(i int64, base int) string

Example

package main

import (
 "fmt"
 "strconv"
)

func main() {
 data := int64(-19)

 str10 := strconv.FormatInt(data, 10)
 fmt.Printf("%T, %v\n", str10, str10)

 str16 := strconv.FormatInt(data, 16)
 fmt.Printf("%T, %v\n", str16, str16)
}

Output

go run app.go

string, -19
string, -13

In this example, we defined the int64 version of the integer variable and assigned a -19 value to the data variable.

Then we tried to convert an integer to a string using the FormatInt() function with the base of 10 and 16. Both return different outputs, as you can see above. This is because we are printing return values data types and their values.

Using Sprintf() function

The fmt.Sprintf() is a Golang function that formats based on the format specifier. The Sprintf() method returns the string.

Syntax

func Sprintf(format string, a ...interface{}) string

Example

package main

import (
 "fmt"
 "io"
 "os"
)

func main() {
 data := 19
 str := fmt.Sprintf("%v \n", data)
 io.WriteString(os.Stdout, str)
}

Output

19

In this example, we are formatting an integer to string using fmt.Sprintf() function and then use the io.WriteString() function to write the contents of the stated string “str” to the writer “w“, which takes a slice of bytes.

Conclusion

Converting from int to string in Go depends on the integer you are working with. You can use either Itoa() function or FormatInt() function for conversion.

That’s it for this tutorial.

Leave a Comment