How to fetch an Integer variable as String in Go?
The Go strconv package has methods which returns string representation of integer variable. You can't convert an integer variable in to string variable. You have to store value of integer variable as string in string variable.
Example
package main
import (
"strconv"
"fmt"
)
func main() {
fmt.Println("\n#######################\n")
var intVar int
intVar = 258
str1 := strconv.Itoa(intVar)
fmt.Printf("%T %v\n",str1,str1)
fmt.Println("\n#######################\n")
str2 := strconv.FormatUint(uint64(intVar), 10)
fmt.Printf("%T %v\n",str2,str2)
fmt.Println("\n#######################\n")
str3 := strconv.FormatInt(int64(intVar), 10)
fmt.Printf("%T %v\n",str3,str3)
fmt.Println("\n#######################\n")
fmt.Println("str1+str2+str3 :",str1 + str2 + str3)
}
FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.
Itoa is shorthand for FormatInt(int64(i), 10).
Most Helpful This Week
Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
Regular expression to extract text between square brackets
How to use wildcard or a variable in our URL for complex routing?
How to check if a string contains a numbers in Golang?
How to check if a string contains a substring in Golang?
How to convert Go struct to JSON?