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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// Go program to illustrate How to convert int to String using Ita() function.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token comment">// strconv Package allows us to use other predefined functions like Iota().</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"strconv"</span>
<span class="token punctuation">)</span>

<span class="token comment">// start function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
   <span class="token comment">// declare and initialize a int type variable and assign value to it</span>
   number <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">20</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Type of variable before conversion: %T \nValue : %v\n"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   
   <span class="token comment">//use the strconv method on the the variable to convert it to string and store the result in a seperate variable.</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>Itoa</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
   
   <span class="token comment">// print the result on the screen using fmt.Println() function</span>
   
   <span class="token comment">// check the data type of variable after conversion process.</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Type of variable after conversion : %T \nValue : %v\n"</span><span class="token punctuation">,</span> result<span class="token punctuation">,</span> result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// Go program to illustrate How to convert int to String using Sprintf() function.</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>

<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"reflect"</span>
<span class="token punctuation">)</span>

<span class="token comment">// start function main()</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   
   <span class="token comment">// declare and initialize a int type variable and assign value to it</span>
   number <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">200</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Number ="</span><span class="token punctuation">,</span>number<span class="token punctuation">)</span>
   <span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">reflect<span class="token punctuation">.</span></span>TypeOf</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Type of variable before conversion ="</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span>

   <span class="token comment">// use the strconv method on the the variable to convert it to string and store the result in a</span>
   <span class="token comment">// seperate variable.</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Sprintf</span><span class="token punctuation">(</span><span class="token string">"%s"</span><span class="token punctuation">,</span> number<span class="token punctuation">)</span>
   <span class="token keyword">var</span> res <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">reflect<span class="token punctuation">.</span></span>TypeOf</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Type of Variable = "</span><span class="token punctuation">,</span>res<span class="token punctuation">)</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"Value ="</span><span class="token punctuation">,</span>result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-11-22T12:49:27+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements