Go Boolean Data Type
Go (Golang) – Boolean Data Type (bool)
The Boolean data type in Go is represented by the keyword bool.
It is used to store true or false values and is mainly used in conditions, loops, and logical operations.
1️⃣ Declaration of Boolean Variables
Using var
Using Short Declaration (:=)
2️⃣ Default Value (Zero Value)
If a boolean variable is declared but not initialized, its default value is false.
3️⃣ Boolean in Conditional Statements
if Statement
if–else
4️⃣ Boolean with Comparison Operators
Comparison operators return boolean values.
5️⃣ Logical Operators with Boolean
| Operator | Meaning |
|---|---|
&& |
AND |
| ` | |
! |
NOT |
6️⃣ Boolean in Loops
7️⃣ Boolean as Function Return Type
8️⃣ Boolean Formatting
Use %t with fmt.Printf().
9️⃣ Important Rules
✔ Only true or false allowed
❌ 0 and 1 are not boolean values in Go
Correct:
Summary
-
boolstorestrueorfalse -
Default value is
false -
Used in conditions, loops, functions
-
Strongly typed (no implicit conversion)
