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

// Go language program to illustrate How to convert Boolean to String package main // import the required packages import ( "fmt" "strconv" ) // fmt package allows us to print anything. // strconv package allows us to use other predefined functions like FormatBool() // This is the main Function func main() { // Initialize a Boolean variable and assign a value to it var i bool i = false // call the FormatBool() function and pass the Boolean variable as an argument in it. // storing the results in a variable string := strconv.FormatBool(i) // Print the result on the screen fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string) // again assign a value to Boolean variable i = true // call the FormatBool() function and pass the Boolean variable as an argument to it. // storing the results in a variable string = strconv.FormatBool(i) // Print the result on the screen fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string) }

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

// Go language program to illustrate How to convert Boolean to String using fmt package package main // import the required packages import ( "fmt" ) // fmt package allows us to print anything on the screen // This is the main Function func main() { // initialize a variable named x of Boolean data type var x bool // assigning value to x variable x = true // use the Sprintf() function to print the formatted string // storing the result in string variable string := fmt.Sprintf("%v ", x) // Print the result on the screen fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string, string) // repeat with other values y := false // use the Sprintf() function to get the formatted string string2 := fmt.Sprintf("%v", y) // Print the result on the screen fmt.Printf("Succesfully converted boolean to %T and its value is %v \n", string2, string2) }

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: 14-Nov-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements