How to Convert Int to String type tutorials

Learn how String conversion from Int type in goes language with examples.

int, String types in Golang

In Any programming language, Anything users enter in text fields is always considered a String and saved to Database as Integer. you check my other post on Convert String to Int type. In some cases, we need to convert this Integer to a String. For example, the integer contains the number 167 and will convert this int to a string as “167”.

String and int are primitive data types in the Go language.

Int is a general data type to hold numeric values. The string is a group of characters enclosed in double quotes.

In Go Language, the Compiler will not cast Primitive conversions implicitly like Int to String conversions. The developer needs to write code to handle this.

Thanks to the Standard strconv package from Golang.

strconv is a go standard inbuilt into a package that provides String conversions to or from other data types like int, float, and boolean. You can check my other posts on String to/from Boolean

How to Convert Int to String in Golang?

With the strconv standard package, It is easy to convert Int to String. strconv package provides various conversion methods.

There are 3 ways to convert an integer type to a string in the Go language.

  • strconv FormatInt function
  • strconv Itoa function
  • fmt sprintf format example

How to convert Int to String using strconv FormatInt function Golang example

strconv FormatInt() function takes input values - integer value and base number and returns the number represented string value. You can check the complete post on strconv FormatInt example

Here is the syntax of this

func FormatInt(input int64, base int) string

Parameters to this function:

  • input: actual number to convert to a string type
  • base: base value from 2 to 36.
  • Return values from this function

returned value a represented numeric string value

Here is a complete example of formatting

package main

import (
    "fmt"
    "strconv"
)

func main() {
    input: = int64(58)
    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:

type=int64, output=58
type=string, output=111010
type=string, output=58
type=string, output=3a

How to convert Int to String using gstrconv Itoa function golang example

strconv.Itoa() function is simple form or FormInt, which is equal to FormatInt(int64(i), 10) function. This takes an Integer value and return string type value. Here is a syntax of Itoa function

func Itoa(i int) string

Following is a complete example of the Itoa function

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {
    var input = 20
    output: = strconv.Itoa(input)
    fmt.Println(output)
    fmt.Println("Input type: ", reflect.TypeOf(input))
    fmt.Println("Output type", reflect.TypeOf(output))

}

When the above program is compiled and executed, The output is as follows

A:\Golang\work>go run Test.go
20
Input type:  int
Output type string

How to convert Int to String using golang fmt Sprintf function example

package fmt Sprintf() function accepts any value argument, converts to string type, and finally string returned.
Here is the syntax of Sprintf() function

func Sprintf(format string, input ...interface{}) string

It accepts two parameters- format string with %v and input - any value

package main

import (
    "fmt"
    "reflect"
)

func main() {
    var input = 20
    output: = fmt.Sprintf("%v", input)
    fmt.Println(output)
    fmt.Println("Input type: ", reflect.TypeOf(input))
    fmt.Println("Output type", reflect.TypeOf(output))

}

output:

20
Input type:  int
Output type string

Conclusion

You learned multiple ways to convert numeric to String values in Go language.