Question

How to Print Boolean Values in Go

goboolean

Question

In Go, %d is used as a format specifier for integers. What format specifier should be used to print boolean values such as true or false?

Short Answer

By the end of this page, you will understand how Go prints boolean values, which format specifier to use with the fmt package, and when to use %t versus general printing functions like fmt.Println. You will also see common mistakes and practical examples.

Concept

In Go, boolean values represent one of two states: true or false. They are commonly used in conditions, comparisons, feature flags, validation results, and control flow.

When printing values in Go, the fmt package provides format verbs such as:

  • %d for integers
  • %s for strings
  • %f for floating-point numbers
  • %t for booleans

So, the correct format specifier for a boolean is:

%t

This matters because Go's formatted printing is type-aware. Using the correct verb makes your output clear and prevents formatting errors such as %!d(bool=true), which happens when you use an integer verb for a boolean value.

In real programs, printing booleans is useful for:

  • debugging conditions
  • logging success or failure states
  • displaying validation results
  • checking feature toggles

Go also allows you to print booleans without a format string by using functions like fmt.Println, but %t is the standard choice when you need formatted output.

Mental Model

Think of a format specifier as a label that tells Go how to display a value.

  • %d says: "This value is an integer."
  • %s says: "This value is a string."
  • %t says: "This value is a true/false value."

It is like choosing the right container for an object. If you put a boolean into an integer container, Go will complain in the output because the label does not match the value.

Take Quiz

Syntax and Examples

The main syntax for printing a boolean with formatting is:

fmt.Printf("%t", myBool)

Basic example

package main

import "fmt"

func main() {
    isReady := true
    fmt.Printf("%t\n", isReady)
}

Output:

true

Printing with a message

package main

import "fmt"

func main() {
    isAdmin := false
    fmt.Printf("Admin access: %t\n", isAdmin)
}

Output:

Admin access: false

Using Println instead

If you do not need a format string, you can also write:

package main

import "fmt"

func main() {
    isLoggedIn := true
    fmt.Println(isLoggedIn)
}

This also prints:

true

fmt.Println is convenient for quick output, while fmt.Printf with %t is better when you want structured text.

Step by Step Execution

Consider this example:

package main

import "fmt"

func main() {
    passed := 5 > 3
    fmt.Printf("Passed: %t\n", passed)
}

Here is what happens step by step:

  1. passed := 5 > 3

    • Go evaluates 5 > 3.
    • Since 5 is greater than 3, the result is true.
    • The variable passed now stores the boolean value true.
  2. fmt.Printf("Passed: %t\n", passed)

    • fmt.Printf reads the format string: "Passed: %t\n".
    • Passed: is printed as plain text.
    • %t tells Go to print the next value as a boolean.
    • passed is inserted, so Go prints true.
    • \n moves output to the next line.

Final output:

Passed: true

Real World Use Cases

Boolean printing appears often in everyday Go programs.

Logging validation results

valid := len(password) >= 8
fmt.Printf("Password valid: %t\n", valid)

Showing feature flags

darkModeEnabled := true
fmt.Printf("Dark mode enabled: %t\n", darkModeEnabled)

Debugging API logic

requestAuthorized := false
fmt.Printf("Authorized: %t\n", requestAuthorized)

Reporting test conditions

connected := true
fmt.Printf("Database connected: %t\n", connected)

These examples are useful when checking whether some condition succeeded or failed during program execution.

Take Quiz

Real Codebase Usage

In real Go codebases, developers often print booleans as part of logs, debugging output, and validation checks.

Common patterns include:

Guard clauses and validation

isValid := email != ""
if !isValid {
    fmt.Printf("Invalid email: %t\n", isValid)
    return
}

Error handling context

success := err == nil
fmt.Printf("Operation successful: %t\n", success)

Configuration and feature flags

debugMode := true
fmt.Printf("Debug mode: %t\n", debugMode)

Structured status messages

isCached := false
fmt.Printf("Cache hit: %t\n", isCached)

In production code, developers may also use logging packages instead of fmt, but the idea is the same: booleans are shown as true or false for clarity.

Common Mistakes

A common beginner mistake is using the wrong format verb for a boolean.

Mistake: using %d for a bool

package main

import "fmt"

func main() {
    flag := true
    fmt.Printf("%d\n", flag)
}

Output:

%!d(bool=true)

Why this happens:

  • %d expects an integer
  • flag is a boolean
  • Go shows a formatting mismatch in the output

Correct version:

fmt.Printf("%t\n", flag)

Mistake: forgetting that Println does not use format verbs

Broken example:

fmt.Println("%t", true)

Output:

%t true

Why:

  • fmt.Println prints values directly
  • it does not interpret %t as a formatting instruction

Correct version:

fmt.Printf("%t\n", true)

or:

fmt.Println(true)

Mistake: expecting uppercase output

Go prints booleans as lowercase:

  • true
  • false

If you need custom text such as YES or NO, you must convert it yourself:

if enabled {
    fmt.Println("YES")
} else {
    fmt.Println("NO")
}

Comparisons

Here is a quick comparison of common Go printing approaches for booleans:

ApproachExampleWhen to useNotes
fmt.Printf with %tfmt.Printf("%t\n", ok)When you need formatted outputBest choice for booleans in a format string
fmt.Printlnfmt.Println(ok)Quick simple printingNo format specifier needed
fmt.Sprintf with %tmsg := fmt.Sprintf("Status: %t", ok)When building a string instead of printing directlyUseful for logs and messages

%t vs %v

Go also supports %v, which prints values in a default format:

fmt.Printf("%v\n", true)

This will also print:

true

Comparison:

VerbMeaningBoolean output
%tExplicit boolean formattingtrue / false
%vDefault value formattingtrue / false

For booleans, %t is more specific and clearer when teaching or reading code.

Take Quiz

Cheat Sheet

  • Boolean format specifier in Go: %t
  • Boolean values print as: true or false
  • Use with fmt.Printf:
fmt.Printf("%t\n", value)
  • Quick print without formatting:
fmt.Println(value)
  • Build a formatted string:
msg := fmt.Sprintf("Enabled: %t", value)
  • %v also works for booleans:
fmt.Printf("%v\n", value)
  • Wrong verb example:
fmt.Printf("%d\n", true)

Produces a formatting mismatch because %d is for integers.

Remember

  • %t = boolean
  • %d = integer
  • %s = string
  • Println does not process format verbs

FAQ

What is the format specifier for booleans in Go?

Use %t to print a boolean with fmt.Printf or fmt.Sprintf.

Can I print a boolean with fmt.Println in Go?

Yes. fmt.Println(true) prints true directly and does not need a format specifier.

What happens if I use %d for a boolean in Go?

Go prints a formatting error in the output, such as %!d(bool=true), because %d expects an integer.

Is %v valid for booleans in Go?

Yes. %v prints the default representation of a value, so it will print true or false for booleans.

Should I use %t or %v for booleans?

Use %t when you want to clearly indicate that the value is a boolean. Use %v when you want generic default formatting.

Does Go print booleans as True and False?

No. Go prints them in lowercase: true and false.

Take Quiz

Mini Project

Description

Create a small Go program that prints the results of several boolean checks. This helps you practice using %t with fmt.Printf and understand how boolean expressions are evaluated and displayed.

Goal

Build a program that evaluates a few conditions and prints each result clearly as true or false.

Requirements

  • Create at least three boolean variables or expressions
  • Print each boolean using fmt.Printf with %t
  • Include descriptive text in the output
  • Use at least one comparison expression such as 10 > 5
Take Quiz

Keep learning

Related questions

View more