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

var isActive bool = true
var isLoggedIn bool = false

Using Short Declaration (:=)

isAdmin := true

2️⃣ Default Value (Zero Value)

If a boolean variable is declared but not initialized, its default value is false.

var status bool
fmt.Println(status) // false

3️⃣ Boolean in Conditional Statements

if Statement

age := 20

if age >= 18 {
fmt.Println("Eligible to vote")
}

if–else

isOnline := false

if isOnline {
fmt.Println("User is online")
} else {
fmt.Println("User is offline")
}


4️⃣ Boolean with Comparison Operators

Comparison operators return boolean values.

a := 10
b := 20

fmt.Println(a > b) // false
fmt.Println(a == b) // false
fmt.Println(a != b) // true


5️⃣ Logical Operators with Boolean

Operator Meaning
&& AND
`
! NOT
x := true
y := false

fmt.Println(x && y) // false
fmt.Println(x || y) // true
fmt.Println(!x) // false


6️⃣ Boolean in Loops

running := true

for running {
fmt.Println("Program running")
running = false
}


7️⃣ Boolean as Function Return Type

func isEven(n int) bool {
return n%2 == 0
}

fmt.Println(isEven(10)) // true


8️⃣ Boolean Formatting

Use %t with fmt.Printf().

status := true
fmt.Printf("Status: %t\n", status)

9️⃣ Important Rules

✔ Only true or false allowed
0 and 1 are not boolean values in Go

if 1 { } // ❌ error

Correct:

if isActive { }

Summary

  • bool stores true or false

  • Default value is false

  • Used in conditions, loops, functions

  • Strongly typed (no implicit conversion)

You may also like...