Go int to string
last modified September 16, 2020
Go int to string conversion tutorial shows how to convert integers to strings in Golang.
Go int to string conversion
Integer to string conversion is a type conversion or type casting, where an entity of integer data type is changed into string one.
In Go, we can perform the int to string conversion with the strconv.FormatInt
,
strconv.Itoa
, or fmt.Sprintf
functions.
The strconv
package implements conversions to and from string
representations of basic data types.
Go int to string with Itoa
The Itoa
is a convenience function which convers an integer to a
string.
func Itoa(i int) string
The Itoa
is equivalent to FormatInt(int64(i), 10)
.
package main import ( "fmt" "strconv" ) func main() { var apples int = 6 n := strconv.Itoa(apples) msg := "There are " + n + " apples" fmt.Println(msg) fmt.Printf("%T\n", apples) fmt.Printf("%T\n", n) }
In the example, we convert the number of apples into a string to build a message.
Later, we output the type of the apples
and n
variables.
$ go run int2str.go There are 6 apples int string
This is the output.
Go int to string with strconv.FormatInt
The strconv.FormatInt
returns the string representation of a value
in the given base; where for 2 <= base <= 36.
package main import ( "fmt" "strconv" ) func main() { var file_size int64 = 1544466212 file_size_s := strconv.FormatInt(file_size, 10) msg := "The file size is " + file_size_s + " bytes" fmt.Println(msg) }
In the example, we convert the file_size
variable, which has
type int64
, to a string with strconv.FormatInt
.
$ go run int2str2.go The file size is 1544466212 bytes
This is the output.
Go int to string with fmt.Sprintf
Another way to convert an integer to a string is to use the fmt.Sprintf
function. The function formats according to a format specifier and returns the
resulting string.
package main import ( "fmt" ) func main() { var apples int = 6 msg := fmt.Sprintf("There are %d apples", apples) fmt.Println(msg) }
The fmt.Sprintf
formats a new string; it replaces the %d
specifier with an integer value.
In this tutorial, we have shown how to perform int to string conversions in Go.
List all Go tutorials.