How to Convert Boolean Into a String in Go

Jay Singh Feb 12, 2024
  1. Use the strconv.FormatBool Function to Convert Boolean Into a String in Go
  2. Use the Sprintf Function to Convert Boolean Into a String in Go
  3. Using Map to Convert Boolean Into a String in Go
  4. Conclusion
How to Convert Boolean Into a String in Go

In Go, converting a Boolean value into a string is a common operation with multiple approaches. This process is crucial when dealing with scenarios where Boolean values need to be represented as strings, such as in data formatting or output generation.

This article will introduce the methods to convert a Boolean to a string data type in Go.

Use the strconv.FormatBool Function to Convert Boolean Into a String in Go

In Go, strconv.FormatBool is a function provided by the strconv package. This function is specifically designed to convert a Boolean value into its string representation.

Basic Syntax:

str := strconv.FormatBool(true)

In the syntax, we convert the Boolean value true to its string representation using the strconv.FormatBool function and assigns the result to the variable str.

Code:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	a := true
	str := strconv.FormatBool(a)
	fmt.Println(str)
}

In this code, we initialize a Boolean variable a with the value true. Using the strconv.FormatBool function from the strconv package, we convert this Boolean value into its corresponding string representation.

The result is printed to the console using fmt.Println.

Output:

true

The output will be true, reflecting the successful conversion of the Boolean value to its string equivalent.

In the next example, strconv.FormatBool receives a Boolean value as an argument and transforms it into a string.

Code:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	boolVal := true
	strVal := strconv.FormatBool(boolVal)
	fmt.Printf("Type of strVal: %T\n", strVal)
	fmt.Printf("Type of boolVal: %T\n", boolVal)
	fmt.Println()
	fmt.Printf("Value of strVal: %v\n", strVal)
	fmt.Printf("Value of boolVal: %v", boolVal)
}

In this code, we declare a Boolean variable boolVal with the value true. Utilizing the strconv.FormatBool function, we convert this Boolean value into a corresponding string stored in the variable strVal.

We then use fmt.Printf statements to display the types and values of both strVal and boolVal.

Output:

Type of strVal: string
Type of boolVal: bool

Value of strVal: true
Value of boolVal: true

The output reveals that the type of strVal is a string, as expected, while the type of boolVal is a Boolean. The subsequent lines print the actual values, confirming the successful conversion of the Boolean to its string representation.

Use the Sprintf Function to Convert Boolean Into a String in Go

Using Sprintf to convert a Boolean into a string in Go involves utilizing the fmt.Sprintf function from the fmt package. This function allows us to format a string based on a format specifier, and in this case, it can be used to convert a Boolean value to its string representation.

Basic Syntax:

strVal := fmt.Sprintf("%v", true)

In this syntax, fmt.Sprintf is used with the format specifier %v to convert the Boolean value true to its string representation. The resulting string is stored in the variable strVal.

You can replace true with any Boolean variable or expression that you want to convert to a string using this approach.

Code:

package main

import (
	"fmt"
)

func main() {
	boolVal := false
	strVal := fmt.Sprintf("%v", boolVal)
	fmt.Printf("Type of strVal: %T\n", strVal)
	fmt.Printf("Type of boolVal: %T\n", boolVal)
	fmt.Println()
	fmt.Printf("Value of strVal: %v\n", strVal)
	fmt.Printf("Value of boolVal: %v", boolVal)
}

In this code, we initialize a Boolean variable, boolVal, with the value false. Using the fmt.Sprintf function from the fmt package, we convert this Boolean value into its corresponding string representation, stored in the variable strVal.

The subsequent fmt.Printf statements are employed to display the types and values of both strVal and boolVal.

Output:

Type of strVal: string
Type of boolVal: bool

Value of strVal: false
Value of boolVal: false

The output indicates that the type of strVal is a string, consistent with the formatting operation, while the type of boolVal is a Boolean. The ensuing lines print the actual values, confirming the successful conversion of the Boolean to its string representation.

Using Map to Convert Boolean Into a String in Go

Using a map to convert a Boolean into a string in Go refers to a method where a map is employed to perform a conditional conversion of a Boolean value to its string representation.

In the absence of a traditional ternary operator in Go, this approach utilizes a map to map true to the string "true" and false to the string "false". The map is then accessed using the Boolean value, providing a concise and readable way to achieve the conversion.

Code:

package main

import (
	"fmt"
)

func main() {
	boolVal := false
	strVal := ternaryString(boolVal)
	fmt.Printf("Type of strVal: %T\n", strVal)
	fmt.Printf("Type of boolVal: %T\n", boolVal)
	fmt.Println()
	fmt.Printf("Value of strVal: %v\n", strVal)
	fmt.Printf("Value of boolVal: %v", boolVal)
}

func ternaryString(b bool) string {
	return map[bool]string{true: "true", false: "false"}[b]
}

In this code, we initialize a Boolean variable, boolVal, with the value false. Instead of using traditional conversion functions, we call the ternaryString function to convert this Boolean value into its corresponding string representation, stored in the variable strVal.

The ternaryString function utilizes a map, mapping true to "true" and false to "false", providing a concise and readable way to perform the conversion. The subsequent fmt.Printf statements are used to display the types and values of both strVal and boolVal.

Output:

Type of strVal: string
Type of boolVal: bool

Value of strVal: false
Value of boolVal: false

The output confirms that the type of strVal is a string, and the type of boolVal is a Boolean. The following lines print the actual values, confirming the successful conversion of the Boolean to its string representation.

Conclusion

In conclusion, we explored various approaches to convert Boolean values to strings in Go. The specialized strconv.FormatBool function from the strconv package demonstrated a concise solution.

Another method utilized fmt.Sprintf for flexible string formatting. Additionally, we presented a creative approach using a map for conditional Boolean-to-string conversion.

Ultimately, developers can choose the method that best suits their needs, whether prioritizing simplicity, flexibility, or creativity in their Go code.

Related Article - Go String