×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

How to print boolean value in Golang?

Last Updated : April 20, 2025

Printing boolean value in Golang

Given boolean values, we have to print them.

To print a boolean value using the fmt.Printf() function – we use "%t" format specifier.

Consider the below example –

In this example, we are declaring two variables value1 and value2, and assigning them with the boolean values true and false. And, then printing them using the "%t" format specifier. Also printing the boolean results of some boolean expressions.

Golang code to print boolean value

// Golang program to print boolean values

package main

import (
	"fmt"
)

func main() {
	value1 := true
	value2 := false

	fmt.Printf("value1 = %t\n", value1)
	fmt.Printf("value2 = %t\n", value2)

	// printing boolean values
	// on evaluating boolean expressions
	fmt.Printf("10 == 20 : %t\n", 10 == 20)
	fmt.Printf("10 != 20 : %t\n", 10 != 20)
	fmt.Printf("10 < 20 : %t\n", 10 < 20)
	fmt.Printf("10 > 20 : %t\n", 10 > 20)
}

Output:

value1 = true
value2 = false
10 == 20 : false
10 != 20 : true
10 < 20 : true
10 > 20 : false

Additional Approaches

1. Using if-else statements

We can also use if-else statements to print boolean values based on conditions.

Example – In the following code, we evaluate a condition and print the result using an if-else statement.

// Golang program to print boolean using if-else statement

package main

import (
	"fmt"
)

func main() {
	value := true

	// using if-else statement
	if value {
		fmt.Println("Value is true")
	} else {
		fmt.Println("Value is false")
	}
}

2. Using boolean expressions directly

You can also print boolean values directly by passing expressions into fmt.Printf(). This allows us to print results without assigning values to variables.

Example – In this case, we print the results of several comparisons directly.

// Golang program to print boolean using expressions directly

package main

import (
	"fmt"
)

func main() {
	// printing boolean directly from expressions
	fmt.Printf("5 == 5 : %t\n", 5 == 5)
	fmt.Printf("5 != 10 : %t\n", 5 != 10)
	fmt.Printf("20 < 15 : %t\n", 20 < 15)
}

3. Storing boolean values in variables

Another approach is to store boolean values in variables and then print them. This gives more flexibility when handling boolean data.

Example – In this example, we store boolean results in variables and then print them.

// Golang program to store boolean values in variables

package main

import (
	"fmt"
)

func main() {
	var result1 bool = true
	var result2 bool = false

	// printing boolean values from variables
	fmt.Println("result1 = ", result1)
	fmt.Println("result2 = ", result2)
}

Go: Print Boolean Value Exercise

Select the correct option to complete each statement about printing boolean values in Go.

  1. In Go, a boolean value can be printed using the ___ function.
  2. In Go, a boolean value is printed as ___ for true and ___ for false.
  3. To format the output when printing a boolean value, you can use the ___ function.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.