Sunday, November 25, 2018

Golang Example - strconv FormatInt Function Guide

In this blog post, We are going to learn strconv FormatInt Function with examples in the Go Language.
Golang Example - strconv FormatInt  Function Guide

golang strconv FormatInt function

strconv is a standard package in go language that provides various function implementations for converting a string to types into an int, float, boolean etc..
FormatInt is one of important function that used to convert Integer type to  String type. You can check official documentation is here 
Here is the syntax of this function
func FormatInt(input int64, base int) string
Argument list
input is a numeric integer value of type int64 only
the base is int arguments that are valid values from 2 to 36.
Return type
The string value is returned which contains a numeric value

Function Usage

FormatInt() function accepts and parse integer type and convert based on the base value, returned a string type value.
if base value is less than equal to 2 ie 1,0 or negative number, It throws runtime error - panic: strconv: illegal AppendInt/FormatInt base
Complete Stack trace
panic: strconv: illegal AppendInt/FormatInt base

goroutine 1 [running]:
strconv.formatBits(0x0, 0x0, 0x0, 0x46e, 0x1, 0x65514b0fc43a0000, 0xc000069eb0, 0x8, 0x10, 0xc0000341c0, ...)
        A:/Golang/src/strconv/itoa.go:91 +0x51c
strconv.FormatInt(0x46e, 0x1, 0x9, 0xc000048238)
        A:/Golang/src/strconv/itoa.go:29 +0xd2
main.main()
        A:/Golang/work/Test.go:12 +0x4c
exit status 2
if base value is greater than 2, It converts the integer to String binary which contains 0 and 1 only
s0 := strconv.FormatInt(55, 2)
fmt.Printf("type=%T, output=%v\n", s0, s0) //type=string, output=110111
if the base value is greater than equal 10, the output contains lower case characters 'a' to 'z' which is of the hexadecimal number in a string representation
s0 := strconv.FormatInt(1234, 16)
 fmt.Printf("type=%T, output=%v\n", s0, s0) //type=string, output=46e

Example

Here is an example of converting Integer to different string numeric values
package main

import (
 "fmt"
 "strconv"
)

func main() {
 input := int64(1134)
 fmt.Printf("type=%T, output=%v\n", input, input)
 s0 := strconv.FormatInt(input, 2)
 fmt.Printf("type=%T, output=%v\n", s0, s0)
 s1 := strconv.FormatInt(input, 10)
 fmt.Printf("type=%T, output=%v\n", s1, s1)
 s2 := strconv.FormatInt(input, 16)
 fmt.Printf("type=%T, output=%v\n", s2, s2)

}
Output is
type=int64, output=1134
type=string, output=10001101110
type=string, output=1134
type=string, output=46e

Convert integer types - Int8,Int16,Int 32 to String 

FormatInt always return Int64 value. With int64(int8 value) code, returned Int64 value is converted to string type. Likewise, Int16(),Int32() are used to converted to Int16, Int32 types. Here is an example program to convert to other types if FormatInt accepts value other than Int64 type, It throws compilation error - cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt
input1 := int8(20)
fmt.Printf("type=%T, output=%v\n", input1, input1) s0 := strconv.FormatInt(input1, 10)
output is
# command-line-arguments
.\Test.go:14:25: cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt
The same error occurs for other types Int16 and Int32, Here is an error case
func main() {
 input1 := int8(34)
 input2 := int16(134)
 input3 := int32(1134)
 fmt.Printf("type=%T, output=%v\n", input1, input1)
 s0 := strconv.FormatInt(input1, 10)
 fmt.Printf("type=%T, output=%v\n", input2, input2)
 s1 := strconv.FormatInt(input2, 10)
 fmt.Printf("type=%T, output=%v\n", s1, s1)
 s2 := strconv.FormatInt(input3, 10)
 fmt.Printf("type=%T, output=%v\n", s2, s2)

}
Output is
.\Test.go:14:25: cannot use input1 (type int8) as type int64 in argument to strconv.FormatInt
.\Test.go:16:25: cannot use input2 (type int16) as type int64 in argument to strconv.FormatInt
.\Test.go:18:25: cannot use input3 (type int32) as type int64 in argument to strconv.FormatInt
FormatInt accepts Int64 value only, we need to convert Int8, Int16, Int32 to Int64 types. Followings an complete example
package main

import (
 "fmt"
 "strconv"
)

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

}
Output is
type=int8, output=34
type=string, output=34
type=string, output=134
type=string, output=1134

Related article


EmoticonEmoticon