Go Boolean Data Type

Go Boolean Data Type (bool) – Complete Guide with Examples
In Go (Golang), the Boolean data type is used to represent true or false values. It is mainly used in conditions, loops, comparisons, and decision-making logic.
Go keeps the boolean type simple, strict, and safe.
What Is Boolean Data Type in Go?
The Boolean data type in Go is called bool.
It can store only two values:
truefalse
Example
- Only
trueorfalse - No
0or1like C/C++
Declaring Boolean Variables
Using var
Using Short Declaration
- Clean
- Type inferred automatically
Default (Zero) Value of bool
If a boolean variable is declared but not initialized, its default value is false.
Output
- Prevents unpredictable behavior
Boolean with Conditional Statements
Boolean values are mostly used with if, else, and switch.
- Comparison expressions return boolean values.
Booleans Comparison Operators
| Operator | Meaning |
|---|---|
== | Equal |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example
Output: true
Logical Operators with Boolean
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
Example
Boolean in Loops
- Loop runs while condition is
true
Boolean as Function Return Type
Functions can return boolean values.
Usage
- Clean
- Reusable logic
Boolean with Switch Statement
- Works directly with boolean values
Boolean Formatting Output
Use %t to print boolean values.
What Go Does NOT Allow
- Using integers as booleans
- Comparing boolean with numbers
- Go enforces strict typing
Common Mistakes
- Expecting
0and1as boolean - Forgetting default value is
false Overusing== trueComplex boolean expressions
Best Practices for Boolean in Go
- Use meaningful boolean names (
isValid,hasAccess) - Avoid
== trueor== false Keep conditions readable- Return booleans from helper functions
- Use logical operators wisely
Interview Questions: Go Boolean
1. What are the possible values of bool in Go?true and false.
2. What is the zero value of bool?false.
3. Can integers be used as booleans in Go?
No.
4. Which format verb prints boolean?%t.
5. Can a function return a boolean?
Yes.
Summary
boolstorestrueorfalseDefault value isfalseUsed in conditions and loops- Strict typing prevents errors
- Essential for decision-making
Mastering the Boolean data type in Go is crucial for writing logical, safe, and professional programs
