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:
%dfor integers%sfor strings%ffor floating-point numbers%tfor 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.
%dsays: "This value is an integer."%ssays: "This value is a string."%tsays: "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.
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:
-
passed := 5 > 3- Go evaluates
5 > 3. - Since 5 is greater than 3, the result is
true. - The variable
passednow stores the boolean valuetrue.
- Go evaluates
-
fmt.Printf("Passed: %t\n", passed)fmt.Printfreads the format string:"Passed: %t\n".Passed:is printed as plain text.%ttells Go to print the next value as a boolean.passedis inserted, so Go printstrue.\nmoves 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.
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:
%dexpects an integerflagis 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.Printlnprints values directly- it does not interpret
%tas a formatting instruction
Correct version:
fmt.Printf("%t\n", true)
or:
fmt.Println(true)
Mistake: expecting uppercase output
Go prints booleans as lowercase:
truefalse
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:
| Approach | Example | When to use | Notes |
|---|---|---|---|
fmt.Printf with %t | fmt.Printf("%t\n", ok) | When you need formatted output | Best choice for booleans in a format string |
fmt.Println | fmt.Println(ok) | Quick simple printing | No format specifier needed |
fmt.Sprintf with %t | msg := fmt.Sprintf("Status: %t", ok) | When building a string instead of printing directly | Useful 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:
| Verb | Meaning | Boolean output |
|---|---|---|
%t | Explicit boolean formatting | true / false |
%v | Default value formatting | true / false |
For booleans, %t is more specific and clearer when teaching or reading code.
Cheat Sheet
- Boolean format specifier in Go:
%t - Boolean values print as:
trueorfalse - 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)
%valso 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= stringPrintlndoes 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.
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.Printfwith%t - Include descriptive text in the output
- Use at least one comparison expression such as
10 > 5
Keep learning
Related questions
Are Go Maps Passed by Value or Reference?
Learn Go map parameter and return semantics: maps are passed by value, while copies share underlying data. Know when map pointers are useful.
Authentication for Go REST APIs: Sessions, Tokens, and Middleware
Learn practical authentication patterns for Go REST APIs, including password hashing, server sessions, bearer tokens, middleware, and security rules.
Automatic Build Versioning in Go: Embed Incrementing Build Numbers
Learn how to add automatic build versioning in Go using linker flags, build metadata, CI counters, and Git-based version values.