Golang Program to convert int type variables to String


In this tutorial we will learn how to convert int type variables to string variables using Golang programming language.

A string is defined as the sequence of one or more characters (letters, numbers, or symbols). Computer applications frequently use strings as a data type thus, there is a need to convert strings to numbers or numbers to strings at manny places specially when we are using data entered by the user.

Syntax

func Itoa(x int) string

Itoa() function in go programming language is used to get the string representation of any integer value here it is depicted by x. This function takes an integer value as an argument and returns the corresponding string representation of that number.

INPUT − int type

OUTPUT − string

Example: Golang program to convert int type variables to strings using iota() function

Using the strconv.Itoa() method from the strconv package in the Go standard library, we can convert numbers to strings. A numeric value will be changed into a string value if it is passed into the method's parentheses as a number or variable.

Algorithm

  • STEP 1 − Import the package fmt and strconv.

  • STEP 2 − Start the function main()

  • STEP 3 − Initialize the integer variables and assign appropriate values to them.

  • STEP 4 − Print the data type of variable

  • STEP 5 − Use the strconv.Itoa() function to convert the integer type value to string.

  • STEP 6 − Store the result in a separate variable

  • STEP 7 − Print the result on the screen using fmt.Println() function

Example

// Go program to illustrate How to convert int to String using Ita() function. package main // fmt package provides the function to print anything // strconv Package allows us to use other predefined functions like Iota(). import ( "fmt" "strconv" ) // start function main() func main() { // declare and initialize a int type variable and assign value to it number := 20 fmt.Printf("Type of variable before conversion: %T \nValue : %v\n", number, number) //use the strconv method on the the variable to convert it to string and store the result in a seperate variable. result := strconv.Itoa(number) // print the result on the screen using fmt.Println() function // check the data type of variable after conversion process. fmt.Printf("Type of variable after conversion : %T \nValue : %v\n", result, result) }

Output

Type of variable before conversion: int
Value : 20
Type of variable after conversion : string
Value : 20

The quotation marks around the number 12 indicate that it is now a string value rather than an integer.

Description of the Code

  • In the above program, we first declare the package main.

  • We imported the fmt package that includes the files of package fmt. We also imported Package strconv implements conversions to and from string representations of basic data types.

  • Next we start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.

  • Now we initialize a int type variable and store a value.

  • Next we call the function Itoa() and pass the respective integer value as an argument to it. It converts the int type variable to a string type variable.

  • Store the result in a separate variable and print the result on the screen using fmt.Println() function.

  • To print the result we can contrast the data types of the variable before and after the conversion process.

Example: Golang program to convert int type variables to strings using Sprintf() function:

Syntax

func Sprintf(format string, a ...interface{}) string

This function returns a formatted string. The first argument should be a string format followed by a variable number of arguments. This function then returns the result as a formatted string format.

Algorithm

  • STEP 1 − Import the package fmt and strconv package.

  • STEP 2 − Start the function main()

  • STEP 3 − Initialize the integer variable and assign value to it.

  • STEP 4 − Print the type of variable.

  • STEP 5 − Use the Sprintf() function to convert integer type value to string.

  • STEP 6 − Print the type of variable again after the conversion process.

Example

// Go program to illustrate How to convert int to String using Sprintf() function. package main // fmt package provides the function to print anything import ( "fmt" "reflect" ) // start function main() func main() { // declare and initialize a int type variable and assign value to it number := 200 fmt.Println("Number =",number) var num = reflect.TypeOf(number) fmt.Println("Type of variable before conversion =",num) // use the strconv method on the the variable to convert it to string and store the result in a // seperate variable. result := fmt.Sprintf("%s", number) var res = reflect.TypeOf(result) fmt.Println("Type of Variable = ",res) fmt.Println("Value =",result) }

Output

Number = 200
Type of variable before conversion = int
Type of Variable = string
Value = %!s(int=200)

Description of the Code

  • First declare the package main.

  • Import the fmt package that allows us to print anything on the screen.

  • Now call the main() function this is the entry point of the executable program. This function does not return anything nor accepts any argument.

  • Now initialize a variable called number of type integer and assign appropriate value to it.

  • Now use Sprintf() function to convert the integer type value to string and pass the integer number as an argument to it.

  • Store the output in a separate variable here we have named it as result.

  • Print the data type of the result variable along with the value it possesses on the screen using fmt.Ptintln() function.

Conclusion

We have successfully compiled and executed the Golang program code to convert int type variables to string type variables along with the examples.

Updated on: 22-Nov-2022

15K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements