ADVERTISEMENT
ADVERTISEMENT

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

Parameter(s):

  • 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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.