What is the strconv.FormatInt() Function in Golang

The strconv.FormatInt() is a built-in Golang function that 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

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.

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

In this example, we assign a negative integer variable to a dataInt variable and then print its value and data type.

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

After passing this variable to the strconv.FormatInt() function 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 the 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

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.

Leave a Comment