How to convert Int to String type in GoLang | GoLang Tutorial

5 years ago Lalit Bhagtani 0

In this tutorial, we will learn about how to convert int to string type in Go programming language.

FormatInt

The function FormatInt converts the integer type value in the given base (2 to 36) to a string type. It uses the lower case letters ‘a’ to ‘z’ for digit values greater than 10, like in case of hexadecimal values ‘a’ is used to represent 11 digit.

Syntax

func FormatInt(i int64, base int) string

Example

package main

import (
    "fmt"
    "strconv"
)

func main() {

    var (
	decimal     int64 = 22
	hexaDecimal int64 = 22
	binary      int64 = 22
    )

    first := strconv.FormatInt(decimal, 10)
    fmt.Printf("%T, %v\n", first, first)

    second := strconv.FormatInt(hexaDecimal, 16)
    fmt.Printf("%T, %v\n", second, second)

    third := strconv.FormatInt(binary, 2)
    fmt.Printf("%T, %v\n", third, third)
}

Output

string, 22
string, 16
string, 10110

References

  1. FormatInt function Docs

That’s all for how to convert int to string type in Go programming language. if you liked it, please share your thoughts in comments section and share it with others too.