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 are the functions used to convert an integer to string:
1. strconv.Itoa(): To convert an int to a decimal string.
// s == "60"
s := strconv.Itoa(60)
2. strconv.FormatInt(): To format an int64 in a given base.
var n int64 = 60
s := strconv.FormatInt(n, 10) // s == "60" (decimal)
s := strconv.FormatInt(n, 16) // s == "3C" (hexadecimal)
3. strconv.FormatUint(): To return the string representation of x in the given base, i.e., 2 <= base <= 36.
fmt.Println(strconv.FormatUint(60, 2)) // 111100 (binary)
fmt.Println(strconv.FormatUint(60, 10)) // 60 (decimal)
Example 1:
Output:
string 23
string 23
string 23
Concatenating all strings: 232323
Example 2:
Output:
string 50
string 110010
string 32
Concatenating all strings: 5011001032