2 Easy Ways to Convert Boolean to String in Golang

Go boolean value is a value that can only be true or false.

Golang string is a built-in data type that can store a sequence of characters.

Let’s use multiple approaches to convert a boolean to a string.

Golang boolean to string

2 easy ways to convert a boolean value to a string in Golang.

  1. Using the strconv.FormatBool() function
  2. Using the fmt.Sprint() function

Technique 1: Using strconv.FormatBool() function

The strconv.FormatBool() is a built-in function that takes a Boolean value as an argument and returns a string representation of that value.

Use the strconv.FormatBool() function to convert a boolean to string, e.g. stringValue := strconv.FormatBool(booleanValue).

Syntax

func FormatBool(x bool) string

Parameters

The FormatBool() function takes “x” as a single required parameter of the bool type.

Example

package main

import (
  "fmt"
  "reflect"
  "strconv"
)

func main() {
  booleanValue := true

 // Convert Boolean to string
  stringValue := strconv.FormatBool(booleanValue)

 // Print result
  fmt.Println(stringValue)

 // Print the data type of a variable 
 fmt.Println("The data type of string value is: ", reflect.TypeOf(stringValue))
}
Output
true
The data type of string value is: string

The strconv.FormatBool() function takes a Boolean value as an argument and returns a string representation of that value.

In the above code, we assigned the Boolean value true to the variable booleanValue and then used that variable as an argument in the strconv.FormatBool() function to convert it to a string.

We also used the reflect.TypeOf() function to check the data type of the variable stringValue.

The reflect.TypeOf() function takes an interface{} type variable as an argument which is “stringValue” in our case, and returns its data type. So, it returned a string.

Technique 2: Using fmt.Sprint() function

The fmt.Sprintf() represents the string by formatting according to a format specifier.

You can use the fmt.Sprintf() function to convert a boolean value to a string in Golang.

Syntax

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

Parameters

format string: It includes some verbs along with some strings.

a …interface{}: It is the specified constant variable.

Example

package main

import (
  "fmt"
)

func main() {
  booleanValue := false

  stringValue := fmt.Sprintf("%v", booleanValue)
  fmt.Printf("Type : %T \nValue : %v\n\n", stringValue, stringValue)

  boolValue := true
  strValue := fmt.Sprintf("%v", boolValue)
  fmt.Printf("Type : %T \nValue : %v\n", strValue, strValue)
}
Output
Type : string
Value : false

Type : string
Value : true

In the above code, the fmt.Sprintf() function created a string representation of the given arguments, formatted according to the format string.

The format string is “%v”, a general format representing the value in a default format.

We assigned the false boolean value to the variable booleanValue and then passed it as an argument to the fmt.Sprintf() function returns the string representation of that value, which is “false“.

We used the fmt.Printf(“Type : %T \nValue : %v\n\n”, stringValue, stringValue) to print the variable type and value, which are “string” and “false”.

Similarly, we assigned the boolean value true to the variable boolValue and then passed it as an argument to the fmt.Sprintf() function returns the string representation of that value, which is “true”.

We used the fmt.Printf(“Type: %T \nValue: %v\n”, strValue, strValue) to print the type and value of the variable, which is “string” and “true”.

Conclusion

To convert a boolean to a string in Golang, you can use the built-in strconv.FormatBool() function. Apart from the strconv.FormatBool() function, you can also use the fmt.Sprintf() function.

Further reading

Golang Int to String

Byte Array to String in Golang

Leave a Comment