How to Print Boolean Value in Go

To print a boolean value in Go, you can “use the fmt.Println() or fmt.Printf() functions. In Go, the fmt package provides various functions for formatted I/O operations. Among these, fmt.Println() and fmt.Printf() is commonly used for printing.

Method 1: Using the fmt.Println() function

The fmt.Println() function prints the arguments it receives and appends a newline character at the end. It can be used to print a boolean value directly:

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.

Related posts

Go fmt.Sscan()

Print the Values of Slices of Go