The Easy Way to Convert Golang Int to String!

Working with numbers in Go, you often need to convert them to strings. The standard way to do this is to use the strconv package. The strconv package contains several functions for converting between strings and other types, including ints.

What is Golang String?

Golang string is a sequence of variable-width characters where each character is defined by one or more bytes using UTF-8 Encoding.

What is an integer in Golang?

Go provides numerous different types to represent the numbers. When we speak of numbers, we divide them 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 int to string

To convert an int to string in Golang,

  1. strconv.Itoa(): The strconv.Itoa() function converts an integer base 10 value to an 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 Itoa() is a built-in function that returns the string representation of input when integer base 10 value. In addition, goo provides a strconv package that executes conversions to and from string representations of basic data types.

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

The FormatInt() is a built-in function of the strconv package that converts an integer value to a string in Golang. The FormatInt() function returns the string representation of i in the given base for 2 <= base <= 36. The result uses the lowercase 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 built-in Golang function that formats based on the specifier. The Sprintf() method accepts the format and string and returns the formatted 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 format an integer to a 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 the FormatInt() function for conversion.

That’s it for this tutorial.

Leave a Comment