Learn how String conversion from Int type in goes language with examples.

int, String types in Golang
In Any programming language, Anything users enter in text fields is always considered as String and saved to Database as Integer. you check my other post on Convert String to Int type. In some cases, we need to convert this Integer to String. For example, the integer contains numbers 167 and will convert this int to string as “167”.
String and int are primitive data types in the Go language. Int is a general data type to hold numeric values. The string is a group of characters enclosed in double-quotes. In Go Language, the Compiler will not cast Primitive conversions implicitly like Int to String conversions. The developer needs to write a code to handle this.
Thanks to the Standard strconv package from Golang. strconv is a go standard inbuilt in a package that provides String conversions to or from other data types like int, float, and boolean. You can check my other posts on String to/from Boolean
How to Convert Int type to String in String
With the strconv standard package, It is easy to convert Int to String. strconv package provides various implementations
There are 3 ways to convert an integer type to a string in the Go language.
- strconv FormatInt function
- strconv Itoa function
- fmt sprintf format example
strconv FormatInt function example
strconv FormatInt() function takes input values - integer value and base number and returns number represented string value. You can check complete post on strconv FormatInt example
Here is the syntax of this
func FormatInt(input int64, base int) string
Parameters to this function:
- input: actual number to convert to a string type
- base: base value from 2 to 36.
- Return values from this function
returned value a represented numeric string value
Here is a complete example of formatting
package main
import (
"fmt"
"strconv"
)
func main() {
input := int64(58)
fmt.Printf("type=%T, output=%v\n", input, input)
s0 := strconv.FormatInt(input, 2)
fmt.Printf("type=%T, output=%v\n", s0, s0)
s1 := strconv.FormatInt(input, 10)
fmt.Printf("type=%T, output=%v\n", s1, s1)
s2 := strconv.FormatInt(input, 16)
fmt.Printf("type=%T, output=%v\n", s2, s2)
}
Output is
type=int64, output=58
type=string, output=111010
type=string, output=58
type=string, output=3a
strconv Itoa function example
strconv.Itoa() function is simple form FormInt equal to FormatInt(int64(i), 10) function. This takes an Integer value and return string type value Here is a syntax of Itoa function
func Itoa(i int) string
Following is a complete example of the Itoa function
package main
import (
"fmt"
"reflect"
"strconv"
)
func main() {
var input = 20
output := strconv.Itoa(input)
fmt.Println(output)
fmt.Println("Input type: ", reflect.TypeOf(input))
fmt.Println("Output type", reflect.TypeOf(output))
}
When the above program is compiled and executed, The output is as follows
A:\Golang\work>go run Test.go
20
Input type: int
Output type string
fmt Sprintf function example
package fmt Sprintf() function which accepts any value argument and converts to string type and finally returned.
Here is a syntax of Sprintf() function
func Sprintf(format string, input ...interface{}) string
It accepts two parameters- format string with %v and input - any value
package main
import (
"fmt"
"reflect"
)
func main() {
var input = 20
output := fmt.Sprintf("%v", input)
fmt.Println(output)
fmt.Println("Input type: ", reflect.TypeOf(input))
fmt.Println("Output type", reflect.TypeOf(output))
}
output is
20
Input type: int
Output type string
Conclusion
You learned multiple ways to convert numeric to String values in Golanguage