How to Use strconv.FormatInt() Function in Go

Golang strconv.FormatInt() function isused to obtain the string representation of a given integer in a chosen base, which can range from 2 to 36 (2 <= base <= 36)”

Syntax

func FormatInt(i int64, base int) string

Parameters

  1. i: The integer value to be converted in the string format.
  2. base: The type of integer represents which base you want to convert.

Return Value

It 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.

Example 1

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

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

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

If strconv.FormatInt() function accepts input value other than Int64 data type, and 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
cannot use in1 (type int8) as type int64 in argument to strconv.FormatInt
cannot use in2 (type int16) as type int64 in argument to strconv.FormatInt
cannot use in3 (type int32) as type int64 in argument to strconv.FormatInt

FormatInt() method only accepts Int64 value.

To fix the cannot use in1 (type int8) as type int64 in argument to strconv.FormatInt error, convert Int8, Int16, and 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.