Golang FormatInt: strconv.FormatInt() Function Guide

The strconv package in Go provides numerous methods for converting to and from string representations of basic data types. 

Golang FormatInt

The strconv.FormatInt() is a Golang function used to convert an integer data type to a string data type. The FormatInt() function accepts an integer and converts based on the base value, returning a string data type value.

Syntax

func FormatInt(i int64, base int) string

Arguments

The FormatInt() takes an int64 as an argument to be converted into a string.

Another parameter is the base of the type integer, which represents which type of base you want to convert.

Return Value

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.

Example

package main

import (
 "fmt"
 "strconv"
)

func main() {
 dataInt := int64(-21)

 stringBase10 := strconv.FormatInt(dataInt, 10)
 fmt.Printf("Value: %v \n", stringBase10)
 fmt.Printf("Data Type: %T \n", stringBase10)

 stringBase2 := strconv.FormatInt(dataInt, 2)
 fmt.Printf("Value: %v \n", stringBase2)
 fmt.Printf("Data Type: %T \n", stringBase2)
}

Output

Value: -21
Data Type: string
Value: -10101
Data Type: string

In this example, we are assigning a negative integer variable to a dataInt variable and then printing its value and data type.

We want to convert an integer to a string on the base 10. So while converting from string to integer using FormatInt() function, we will pass 10 as a second argument because it represents the base.

After passing this variable to the strconv.FormatInt() function, it returns the string representation of that value based on the provided base, which in our case is 10.

In the second part of that code, we passed 2 as a base to the FormatInt() function, which returns the string representation based on base 2.

Example 2

package main

import (
 "fmt"
 "strconv"
)

func main() {
 var x int64
 var y int64
 var res string

 x = 21
 res = strconv.FormatInt(x, 10)
 fmt.Printf("Value: %v \n", res)
 fmt.Printf("Data Type: %T \n", res)

 y = 100
 res = strconv.FormatInt(y, 2)
 fmt.Printf("Value: %v \n", res)
 fmt.Printf("Data Type: %T \n", res)
}

Output

Value: 21
Data Type: string
Value: 1100100
Data Type: string

FormatInt() method always returns Int64 value. With int64(int8 value) code, returned Int64 value is converted to string type.

cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt in Golang

If strconv.FormatInt() function accepts input value other than of Int64 data type, it throws the compilation error – Cannot use in1 (type int8) as type int64 in argument to strconv.FormatInt.

package main

import (
 "fmt"
 "strconv"
)

func main() {
 in1 := int8(34)
 in2 := int16(134)
 in3 := int32(1134)
 fmt.Printf("type=%T, output=%v\n", in1, in1)
 s0 := strconv.FormatInt(in1, 10)
 fmt.Printf("type=%T, output=%v\n", s0, s0)
 s1 := strconv.FormatInt(in2, 10)
 fmt.Printf("type=%T, output=%v\n", s1, s1)
 s2 := strconv.FormatInt(in3, 10)
 fmt.Printf("type=%T, output=%v\n", s2, s2)
}

Output

# command-line-arguments
./app.go:13:25: cannot use in1 (type int8) as type int64 in argument to strconv.FormatInt
./app.go:15:25: cannot use in2 (type int16) as type int64 in argument to strconv.FormatInt
./app.go:17:25: cannot use in3 (type int32) as type int64 in argument to strconv.FormatInt

FormatInt() method only accepts Int64 value.

To solve the cannot use in1 (type int8) as type int64 in argument to strconv.FormatInt error in the above example, we need to convert Int8, Int16, Int32 to Int64 types.

package main

import (
 "fmt"
 "strconv"
)

func main() {
 in1 := int8(34)
 in2 := int16(134)
 in3 := int32(1134)
 fmt.Printf("type=%T, output=%v\n", in1, in1)
 s0 := strconv.FormatInt(int64(in1), 10)
 fmt.Printf("type=%T, output=%v\n", s0, s0)
 s1 := strconv.FormatInt(int64(in2), 10)
 fmt.Printf("type=%T, output=%v\n", s1, s1)
 s2 := strconv.FormatInt(int64(in3), 10)
 fmt.Printf("type=%T, output=%v\n", s2, s2)
}

Output

type=int8, output=34
type=string, output=34
type=string, output=134
type=string, output=1134

That’s it for this tutorial.

See also

Golang strconv.Itoa()

Converting String to Int in Go

Leave a Comment