- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang Program to convert boolean variables into string
In this tutorial, we will learn how to convert Boolean to a string in Go programming language. Boolean is a data type having 1 byte size. It can store any one of the three values mainly True, False or none. String data type is used to store a sequence of characters.
Convert Boolean Variables to String using FormatBool() Functions
Syntax
func FormatBool(b bool) string
This function is present in strconv package. FormatBool() function is used to convert a Boolean variable to string. The function takes the Boolean value as an argument and returns a string.
Algorithm
STEP 1 − Import the fmt and strconv package.
STEP 2 − Start the main() function.
STEP 3 − Initialize a variable of type Boolean and assign a value to it either true or false.
STEP 4 − Now, call the FormatBool() function present in strconv package by passing the Boolean variable as argument to the function and store the result obtained by this function in a variable.
STEP 5 − Now, print the result on the screen using fmt.Printf() function.
STEP 6 − Repeat the steps by changing the value of Boolean variable.
Example
In this program we are writing a go language code to convert Boolean variables to strings using a predefined function called FormatBool().
package main import ( "fmt" "strconv" ) func main() { var i bool i = false string := strconv.FormatBool(i) fmt.Printf("Succesfully converted boolean to %T and its value is %v\n", string, string) i = true string = strconv.FormatBool(i) fmt.Printf("Successfully converted boolean to %T and its value is %v\n", string, string) }
Output
Succesfully converted boolean to string and its value is false Successfully converted boolean to string and its value is true
Convert a Boolean Variable to String using 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.
Algorithm
STEP 1 − Import the fmt package.
STEP 2 − Start the main() function.
STEP 3 − Initialize a variable of type Boolean and assign value to it.
STEP 4 − Call the Sprintf() function by passing the Boolean variable as argument to the function and store the result obtained in a variable called result.
STEP 5 − Print the final result on the screen using fmt.Println() function.
STEP 6 − Repeat the above steps by changing the value of Boolean variable and print the result.
Example
In this program we are writing a go language code to convert Boolean variables to strings using a predefined function called FormatBool().
package main import ( "fmt" ) func main() { var x bool x = true string := fmt.Sprintf("%v ", x) fmt.Printf("Succesfully converted boolean to %T and its value is %v\n", string, string) y := false string2 := fmt.Sprintf("%v", y) 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
Conclusion
We have successfully compiled and executed the Golang program to convert a Boolean variable to a string using internal functions along with the examples. In the first example, we used the FormatBool() function present in the strconv package, and in the second one, we used Sprintf() function to achieve the result.
- Related Articles
- Golang Program to convert string type variables into Boolean
- Haskell Program to convert boolean variables into string
- C++ Program to convert Boolean Variables into String
- C++ Program to Convert String Type Variables into Boolean
- Haskell Program to convert string type variables into boolean
- Golang Program to convert string type variables into int
- Golang Program to convert Boolean to String
- Swift program to convert boolean variable into string
- Golang Program to convert string variables to double
- Swift program to convert string type variable into boolean
- Golang Program to convert double type variables to string
- Golang Program to convert int type variables to String
- C++ Program to Convert int Type Variables into String
- Haskell Program to convert string type variables into int
- Golang Program to convert a string into Uppercase
