Integer variable cannot be directly convert into String variable. In order to convert string to integer type in Golang , you have store value of integer variable as string in string variable. For this, we are using strconv and fmt package functions.
1. Itoa() Function: The Itoa stands for Integer to ASCII and returns the string representation of integer with base 10.
Syntax:
func Itoa(i int) string
Example : Convert an integer variable into String using strconv.Itoa() function.
Go
// Go program to illustrate // How to convert integer // variable into String package main import ( "fmt" "strconv") //Main Function func main() { i := 32 s := strconv.Itoa(i) fmt.Printf("Type : %T \nValue : %v\n", s, s) } |
Output:
Type : string Value : 32
2. FormatInt() Function: The FormatInt is used to represents the string of integer value with base [2,36]. The result uses the lower-case letters ‘a’ to ‘z’ for digit values >= 10.
Syntax:
func FormatInt(i int64, base int) string
Example : Convert an integer variable into String using strconv.FormatInt() function.
Go
// Go program to illustrate // How to convert integer // variable into String package main import ( "fmt" "strconv") //Main Function func main() { i := -32 s := strconv.FormatInt(int64(i) , 7) fmt.Printf("Type : %T \nValue : %v\n", s, s) } |
Output:
Type : string Value : -44
3. FormatUint() Function: The FormatUint is used to represents the string of integer value with base [2,36]. The result uses the lower-case letters ‘a’ to ‘z’ for digit values >= 10. It is similar to FormatInt but the difference is that uint64 is the type of integer value.
Syntax:
func FormatUint(i uint64, base int) string
Example : Convert an integer variable into String using strconv.FormatUint() function.
Go
// Go program to illustrate // How to convert integer // variable into String package main import ( "fmt" "strconv") //Main Function func main() { i := 32 s := strconv.FormatUint(uint64(i) , 7) fmt.Printf("Type : %T \nValue : %v\n", s, s) } |
Output:
Type : string Value : 44
4. Sprintf() Function: The Sprintf is used to represents the string of integer value by formatting according to a format specifier and returns the resulting string.
Syntax:
func Sprintf(format string, a ...interface{}) string
Example : Convert an integer variable into String using fmt.Sprintf() function.
Go
// Go program to illustrate // How to convert integer // variable into String package main import ( "fmt") //Main Function func main() { i := 32 s := fmt.Sprintf("%d", i) fmt.Printf("Type : %T \nValue : %v\n", s, s) } |
Output:
Type : string Value : 32
Recommended Posts:
- Different Ways to Find the Type of Variable in Golang
- Different Ways to Convert the Boolean Type in String in Golang
- Converting a string variable into Boolean, Integer or Float type in Golang
- How to fetch an Integer variable as String in GoLang?
- How to Convert string to integer type in Golang?
- Different ways to concatenate two strings in Golang
- Different Ways to Find the Type of an Object in Golang
- Different ways to compare Strings in Golang
- Atomic Variable in Golang
- Golang program that uses func with variable argument list
- How to declare and access pointer variable in Golang?
- Golang Program that uses func as Local Variable
- How to convert a string in uppercase in Golang?
- How to convert a string in lower case in Golang?
- How to Convert a zero terminated byte array to string in Golang?
- How to Convert string to float type in Golang?
- Finding the Integer Value of Specified Number in Golang
- How to use Array Reverse Sort Functions for Integer and Strings in Golang?
- How to compare Structs with the Different Values Assigned to Data Fields in Golang?
- Different Types of Recursion in Golang
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

