Example 1: Golang Bool to String Using the FormatBool() Method
Specifically, we may use the strconv.FormatBool() function from the “strconv” library to transform a Boolean value into a string representation.
Take a look at the following example where the FormatBool() function is used.
import (
"fmt"
"reflect"
"strconv"
)
func main() {
boolValue := true
strValue := strconv.FormatBool(boolValue)
fmt.Println(strValue)
fmt.Println("The type of string value is:", reflect.TypeOf(strValue))
}
In the previous program, the “strconv” package is imported to convert the Boolean to the string. There, we also defined another package which is “reflect” to retrieve the type of the resulting string. We established the “boolValue” variable of type “bool” which is initialized with the “true” value. We then created another variable which is “strValue” of type “string” that converts the Boolean value.
We setup this variable with the strconv.FormatBool function which stores the “boolValue” inside it as an argument. The strconv.FormatBool() function converts a Boolean value to its corresponding string representation: “true” for true and “false” for false. The string value is “true” in this instance since the “boolValue” is true. Lastly, we used the reflect.TypeOf()function to obtain the type information of the “strValue”.
Since the “strValue” is string, the output displays the typed string as follows:
Example 2: Golang Bool to String Using the Fmt.Sprintf() Method
Additionally, the fmt.Sprintf() function in Go also allows us to transform the Boolean values to strings using the the%v verb and generate the value in accordance with its default format. Consider the working of the Sprintf() function:
import (
"fmt"
)
func main() {
b1 := true
s1 := fmt.Sprintf("%v", b1)
fmt.Printf("Type : %T \nValue : %v\n\n", s1, s1)
b2 := false
s2 := fmt.Sprintf("%v", b2)
fmt.Printf("Type : %T \nValue : %v\n", s2, s2)
}
In the given program, we defined a variable “b1” and assigned the Boolean value of “true” where it represents a true condition. After that, we employed the fmt.Sprintf() function which converts the Boolean value of “b1” to a string using the “%v” format specifier. This format specifier is a general placeholder that represents the value in its default format. We stored the resulting value in the “s1” variable.
The fmt.Printf() function is then called to print the type and value of “s1” to the console. The “%T” format specifier here is used to print the type, and “%v” is used to print the value. Then, we also declared the Boolean variable of “b2” with the value of “false”. Here, it represents a false condition. Similarly, we deployed the fmt.Sprintf() function to convert the Boolean value of “b2” to a string using the “%v” format specifier and the resulting string is stored in the “s2” variable.
The output shows that both “s1” and “s2” have a “string” type. For “s1”, the value is true. While for “s2”, the value is false:
Example 3: Golang Bool to String Using If-Else Statement
Moreover, we can manually examine the Boolean value and give its associated string representation if we want to convert a Boolean value in Go to a string using an if-else expression.
import "fmt"
func main() {
boolIs := false
stringIs := func() string {
if boolIs {
return "true"
}
return "false"
}()
fmt.Println(stringIs)
}
The given program creates the “boolIs” variable with the Boolean value of “false”. Then, we declared the “stringIs” variable as an anonymous function for a string result. This function is immediately invoked by adding () after its definition. Inside the anonymous function, an “if” statement checks the value of “boolIs”. The function returns “true” if the given scenario is true. It provides the “false” string in the absence of that. Then, we specified the “stringIs” variable to be a function that provides a string anonymously.
The condition returns the Bool false as the string value of false in the following output:
Example 4: Golang Bool to String Using the Map Method
However, if we specifically want to use the “map” method, we can define a map that maps the Boolean values to their corresponding string representations. Here’s the map program to convert the Bool to a string.
import "fmt"
func main() {
bool1 := true
bool2 := false
boolToString := map[bool]string{
true: "true",
false: "false",
}
trueStr := boolToString[bool1]
falseStr := boolToString[bool2]
fmt.Println(trueStr)
fmt.Println(falseStr)
}
In the given program, the two boolean variables, “bool1” and “bool2”, are declared and assigned with the Boolean values of true and false, respectively. Then, we setup the “boolToString” map with Bool as the key type and string as the value type. It maps the Boolean values to their string representations. After that, the “boolToString” map is initialized with two key-value pairs: The “true” key is mapped to the “true” string, while the “false” key is mapped to the “false” string. Next, we defined the “trueStr” and “falseStr” variables which are assigned with the values that are obtained from the “boolToString” map using the Boolean variables of “bool1” and “bool2”, respectively.
Using a map lookup, the output displays the conversion of Boolean values to their corresponding string representations:
Example 5: Golang String to Bool Using the ParseBool() Method
The prior examples are all transformations of Bool to string type. Here, we use the ParseBool() method to parse a string into the Bool type. The Boolean value that the string represents is returned by the ParseBool() method. The program is given in the following to convert the string to Bool using the ParseBool() function:
import "fmt"
import "strconv"
func main() {
var MyStr string = "false"
var MyBool bool = true
MyBool, err := strconv.ParseBool(MyStr)
if err != nil {
panic(err)
} else {
fmt.Println("Result: ", MyBool)
}
}
In the given program, we declared a “MyStr” variable of type string and assigned it with the “false” value. Then, a bool-type variable named “MyBool” with the “true” value as its initial value is given. Next, we used the strconv.ParseBool() function to parse the string representation of a Boolean value that is stored in “MyStr”. The result of the parsing is assigned to the “MyBool” variable, and any error that is encountered during parsing is assigned to the “err” variable.
The output generates the results where the “false” string is parsed into a Boolean value:
Conclusion
Many Go applications frequently need to transform a Boolean value into a string representation. Here, we discussed the approaches that are useful in transforming the Boolean type into a “string” type. Apart from these examples, we also demonstrated the method to transform the string to the Bool type.