Home »
Golang »
Golang Reference
Golang strconv.Itoa() Function with Examples
Golang | strconv.Itoa() Function: Here, we are going to learn about the Itoa() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 07, 2021
strconv.Itoa()
The Itoa() function is an inbuilt function of the strconv package which is used to get the string representation of the given integer (int). The Itoa() function is equivalent to FormatInt(int64(i), 10).
It accepts a parameter (i) and returns the string representation of the given integer value.
Syntax
func Itoa(i int) string
Parameters
- i : An integer value that is to be converted in the string format.
Return Value
The return type of Itoa() function is a string, it returns the string representation of the given integer value.
Example 1
// Golang program to demonstrate the
// example of strconv.Itoa() Function
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.Itoa(255))
fmt.Println(strconv.Itoa(65535))
fmt.Println(strconv.Itoa(12345678912345))
fmt.Println()
fmt.Println(strconv.Itoa(-255))
fmt.Println(strconv.Itoa(-65535))
fmt.Println(strconv.Itoa(-12345678912345))
}
Output:
255
65535
12345678912345
-255
-65535
-12345678912345
Example 2
// Golang program to demonstrate the
// example of strconv.Itoa() Function
package main
import (
"fmt"
"strconv"
)
func main() {
var x int
var y int
var result string
x = 108
result = strconv.Itoa(x)
fmt.Printf("x: Type %T, value %v\n", x, x)
fmt.Printf("result: Type %T, value %q\n", result, result)
fmt.Println()
y = 64
result = strconv.Itoa(y)
fmt.Printf("y: Type %T, value %v\n", y, y)
fmt.Printf("result: Type %T, value %q\n", result, result)
fmt.Println()
z := x + y
result = strconv.Itoa(z)
fmt.Printf("z: Type %T, value %v\n", z, z)
fmt.Printf("result: Type %T, value %q\n", result, result)
}
Output:
x: Type int, value 108
result: Type string, value "108"
y: Type int, value 64
result: Type string, value "64"
z: Type int, value 172
result: Type string, value "172"
Golang strconv Package »