How to convert an int to a string in Golang

An integer can be converted to a string in Golang using the Itoa function within the strconv library. This library supports conversions to and from string representations to various other data types.

The code snippet below shows how to convert an int to a string using the Itoa function.

package main
import "fmt"
import "strconv"
import "reflect" // The reflect library is used to obtain the datatype of an object.
func main() {
num1:= 10
fmt.Println(num1, reflect.TypeOf(num1))
str:= strconv.Itoa(num1)
fmt.Println(str, reflect.TypeOf(str))
}
Copyright ©2024 Educative, Inc. All rights reserved