Different Ways to Convert an Integer Variable to String in Golang
Last Updated :
28 Apr, 2025
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
Similar Reads
Different Ways to Convert the Boolean Type in String in Golang In order to convert Boolean Type to String type in Golang , you can use the strconv and fmt package function. 1. strconv.FormatBool() Method: The FormatBool is used to Boolean Type to String. It returns "true" or "false" according to the value of b. Syntax: func FormatBool(b bool) string Example : C
2 min read
How to fetch an Integer variable as String in GoLang? To fetch an Integer variable as String, Go provides strconv package which has methods that return a string representation of int variable. There is nothing like an integer variable is converted to a string variable, rather, you have to store integer value as a string in a string variable. Following
2 min read
Different ways to concatenate two strings in Golang In Go, strings are immutable sequences of bytes encoded with UTF-8. Concatenating two or more strings into a single string is straightforward in Go, and there are several ways to accomplish it. In this article,we will learn various ways to concatenate two strings in Golang.ExampleInput:s1 := "Hello,
3 min read
How to Convert string to integer type in Golang? Strings in Golang is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go language, both signed and unsigned integers are available in four different sizes. In order to convert string to integer type in Golang, you can
2 min read
Converting a string variable into Boolean, Integer or Float type in Golang Various types of string conversions are needed while performing tasks in Golang. Package strconv is imported to perform conversions to and from string. String to Boolean Conversion ParseBool is used to convert string to boolean value. It accepts 1, t, T, TRUE, true, True as true and 0, f, F, FALSE,
3 min read