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)
}
Functions which we use for same are :

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