How to Print Boolean Value in Go

To print a boolean value in Go, you can use the fmt.Println() or fmt.Printf() functions.

Method 1: Using the fmt.Println() function

Here is an example of printing a boolean value using fmt.Println() function.

package main

import (
  "fmt"
)

func main() {
  isTrue := true
  fmt.Println("The boolean value is:", isTrue)
}

Output

The boolean value is: true

Method 2: Using the fmt.Printf() function

You can also use the fmt.Printf() function with the %t verb, specifically used to format boolean values.

package main

import (
  "fmt"
)

func main() {
  isTrue := true
  fmt.Printf("The boolean value is: %t\n", isTrue)
}

Output

The boolean value is: true

Remember to use %t as the format specifier for boolean values when using fmt.Printf() function.

That’s it.

Leave a Comment