Convert Int to String

To convert int to string in Go language, call strconv.Itoa() function and pass the integer as argument to it. The function returns the string formed with the given integer.

In this tutorial, we will learn how to convert a given integer to string using Itoa() function.

Syntax

The syntax to call Itoa() function is

strconv.Itoa(x)

where x is an integer.

ItoA() function is present in strconv package. Make sure to import this package before using Itoa().

Return Value

The function returns a string value.

ADVERTISEMENT

Examples

In the following example, we take an integer in variable x, and convert this integer value to string using Itoa() function.

Example.go

package main

import "strconv"

func main() {
	var x int = 25
	result := strconv.Itoa(x)
	println("Output string : " + result)
}

Output

Output string : 25

Now, let us concatenate the result of Itoa() to a string.

Example.go

package main

import "strconv"

func main() {
	var x int = 25
	result := "Hello World - " + strconv.Itoa(x)
	println(result)
}

Output

Hello World - 25

Conclusion

In this Golang Tutorial, we learned how to convert an Int to String value in Go language, with example programs.