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
How to check if a string contains a numbers in Golang?
How to count number of repeating words in a given String?
How to import structs from another package in Go?
Find element in a slice and move it to first position?
Example to create custom error
How to get the current date and time with timestamp in local and other timezones ?
How to use for and foreach loop?
Example to compare Println vs Printf
Different ways to convert Byte Array into String
How to Decode or Unmarshal bi-dimensional array of integers?