How to convert a boolean value to a string using strconv.FormatBool() in Golang?

To convert a boolean value to a string in Go using the strconv.FormatBool() function, you can follow these steps:

  1. Import the strconv package by adding this line at the beginning of your code:
import "strconv"
  1. Declare a boolean variable and assign it a value:
var b bool = true
  1. Use the strconv.FormatBool() function to convert the boolean variable to a string:
str := strconv.FormatBool(b)

The strconv.FormatBool() function returns a string representation of the boolean value. In this example, the variable str will contain the string "true".

Here's a complete example:

package main import ( "fmt" "strconv" ) func main() { var b bool = true str := strconv.FormatBool(b) fmt.Println(str) }

Output:

true