How to Use strconv.Itoa() Function in Golang

Golang strconv.Itoa() is equivalent to the FormatInt(int64(i), 10) function “used to get the string representation of the given integer (int).”

Syntax

func Itoa(i int) string

Parameters

The Itoa() function takes an input i of type integer.

Return Value

The strconv.Itoa() function returns a string that represents i.

Example 1: How to Use strconv.Itoa() function

package main

import (
  "fmt"
  "strconv"
)

func main() {
  str1 := strconv.Itoa(19)
  fmt.Printf("%T, %v\n", str1, str1)

  str2 := strconv.Itoa(21)
  fmt.Printf("%T, %v\n", str2, str2)
}

Output

string, 19
string, 21

Example 2: Passing a negative integer

Pass a negative integer as an input to the strconv.Itoa() function.

package main

import (
 "fmt"
 "strconv"
)

func main() {
 int1 := -19
 fmt.Printf("Output: %d \n", int1)
 fmt.Printf("Data Type: %T \n", int1)
 fmt.Println("After conversion:")
 str1 := strconv.Itoa(int1)
 fmt.Printf("Output: %v \n", str1)
 fmt.Printf("Output: %T \n", str1)
}

Output

Output: -19
Data Type: int
After conversion:
Output: -19
Output: string

That’s it.

Related posts

Go strconv.FormatUInt()

Go strconv.atoi()

Go strconv.AppendInt()

Go strconv.AppendFloat()

Go strconv.ParseInt()

Leave a Comment