Golang Program to convert Boolean to String

In this tutorial, we will learn how to convert Boolean to a string in Go programming language.

Boolean Vs String

Boolean is a data type having 1 byte size. It can store any one of the three values mainly True, False or none. It act like a flag to show whether a condition is correct or not.

String data type is used to store a sequence of characters. it can be in the form of literals or alphabets. The size of string variable is 1 byte or 8 bits.

Example 1: Convert Boolean type to String USING strconv.FormatBool() Method

Syntax

func FormatBool(b bool) string

The function FormatBool() is used to convert a Boolean variable to string. This function takes the Boolean value as an argument and returns a string which we can easily store inside a variable and print on the screen. This function is present in strconv method. So in order to use this function we have to first import the strconv package to our program.

Algorithm

  • Step 1 ? Import the package fmt and strconv

  • Step 2 ? Start function main()

  • Step 3 ? Initialize a Boolean variable and assign a value to it.

  • Step 4 ? Use strconv.FormatBool() function.

  • Step 5 ? Store the result in a variable and print it on the screen.

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 language program to illustrate How to convert Boolean to String</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// import the required packages</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">// fmt package allows us to print anything.</span>
<span class="token comment">// strconv package allows us to use other predefined functions like FormatBool()</span>
<span class="token comment">// This is the main Function</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">// Initialize a Boolean variable and assign a value to it</span>
   <span class="token keyword">var</span> i bool
   i <span class="token operator">=</span> <span class="token boolean">false</span>
   
   <span class="token comment">// call the FormatBool() function and pass the Boolean variable as an argument in it.</span>
   <span class="token comment">// storing the results in a variable</span>
   string <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>FormatBool</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result on the screen</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">"Succesfully converted boolean to %T and its value is %v \n"</span><span class="token punctuation">,</span> string<span class="token punctuation">,</span> string<span class="token punctuation">)</span>
   
   <span class="token comment">// again assign a value to Boolean variable</span>
   i <span class="token operator">=</span> <span class="token boolean">true</span>
   
   <span class="token comment">// call the FormatBool() function and pass the Boolean variable as an argument to it.</span>
   <span class="token comment">// storing the results in a variable</span>
   string <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">strconv<span class="token punctuation">.</span></span>FormatBool</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result on the screen</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">"Succesfully converted boolean to %T and its value is %v \n"</span><span class="token punctuation">,</span> string<span class="token punctuation">,</span> string<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

Succesfully converted boolean to string and its value is false 
Succesfully converted boolean to string and its value is true

Description of the Code

In the above program, first we have to import the fmt and strconv package.

The fmt package allows us to print anything on the screen whereas the strconv package lets us use other predefined functions like FormatBool()

Now start the function main()

Next initialize the variable of type boolean to be converted to string and assign it a value like true or false.

Call the FormatBool() function and pass the Boolean variable as an argument to it. This function will return the string. Store the string returned in a variable.

Now we use the fmt.Println() function to print the result on the screen. Use the %T string formatting flag to determine the datatype of the number entered.

%T? flag is part of "fmt" package.

Example 2: Convert Boolean type to String using fmt.Sprintf() Method

In this example we will be converting a Boolean Type into String using fmt.Sprintf() function.

Syntax

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

This function returns a formatted string. It takes a number of arguments in string format. 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.

  • Step 2 ? Start function main() function.

  • Step 3 ? Initialize a boolean variable i and assign it a value.

  • Step 4 ? use Sprintf() function on the variable

  • Step 5 ? Use the %T string formatting flag to determine the datatype of the number entered.

  • Step 6 ? Print the final results on the screen.

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 language program to illustrate How to convert Boolean to String using fmt package</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// import the required packages</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
<span class="token punctuation">)</span>

<span class="token comment">// fmt package allows us to print anything on the screen</span>
<span class="token comment">// This is the main Function</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">// initialize a variable named x of Boolean data type</span>
   <span class="token keyword">var</span> x bool
   
   <span class="token comment">// assigning value to x variable</span>
   x <span class="token operator">=</span> <span class="token boolean">true</span>
   
   <span class="token comment">// use the Sprintf() function to print the formatted string</span>
   <span class="token comment">// storing the result in string variable</span>
   string <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">"%v "</span><span class="token punctuation">,</span> x<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result on the screen</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">"Succesfully converted boolean to %T and its value is %v \n"</span><span class="token punctuation">,</span> string<span class="token punctuation">,</span> string<span class="token punctuation">)</span>
   
   <span class="token comment">// repeat with other values</span>
   y <span class="token operator">:</span><span class="token operator">=</span> <span class="token boolean">false</span>
   
   <span class="token comment">// use the Sprintf() function to get the formatted string</span>
   string2 <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">"%v"</span><span class="token punctuation">,</span> y<span class="token punctuation">)</span>
   
   <span class="token comment">// Print the result on the screen</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">"Succesfully converted boolean to %T and its value is %v \n"</span><span class="token punctuation">,</span> string2<span class="token punctuation">,</span> string2<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

Succesfully converted boolean to string and its value is true 
Succesfully converted boolean to string and its value is false

Description of the Code

In the above program, we first declare the package main

We imported the fmt package, fmt package allows us to print anything on the screen.

Now start the function main()

Next initialize the variable of Boolean data type to be converted to string.

Call the Sprintf() function and pass the Boolean variable as an argument to it. This function will return the formatted string. Store the string returned in a variable.

Use the %T string formatting flag to determine the datatype of the number entered.

%T? flag is part of "fmt" package.

Store the result into different variable and print the reults on screen using the printf function of fmt package.

Conclusion

We have successfully compiled and executed the Golang program code to convert Boolean value to string using functions along with the examples.

Updated on: 2022-11-14T11:36:09+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements