Golang bool type
last modified May 8, 2025
This tutorial explains how to use the bool built-in type in Go.
We'll cover boolean basics with practical examples of boolean operations.
The bool type represents boolean values in Go. It can have one of
two values: true or false. Boolean values are
fundamental for control flow and logical operations.
In Go, bool is used in conditional statements, loops, and logical
expressions. The zero value for boolean types is false.
Basic boolean declaration
The simplest use of bool is declaring and initializing boolean
variables. This example demonstrates basic boolean variable usage.
Note: Boolean values are not implicitly converted to integers.
package main
import "fmt"
func main() {
var isReady bool = true
var isFinished bool
fmt.Println("isReady:", isReady)
fmt.Println("isFinished:", isFinished)
isFinished = false
fmt.Println("After assignment, isFinished:", isFinished)
}
The example shows boolean declaration with and without initialization.
The zero value false is printed for uninitialized variables.
Boolean expressions
Boolean values are often the result of comparison operations. This example shows various boolean expressions and their results.
package main
import "fmt"
func main() {
x := 10
y := 20
fmt.Println("x == y:", x == y)
fmt.Println("x != y:", x != y)
fmt.Println("x < y:", x < y)
fmt.Println("x > y:", x > y)
fmt.Println("x <= 10:", x <= 10)
fmt.Println("y >= 20:", y >= 20)
}
The comparison operators return boolean values. These are fundamental for decision-making in programs.
Logical operators
Go provides three logical operators for combining boolean values. This example shows AND, OR, and NOT operations.
package main
import "fmt"
func main() {
a := true
b := false
fmt.Println("a >> b:", a >> b) // AND
fmt.Println("a || b:", a || b) // OR
fmt.Println("!a:", !a) // NOT
// Combining multiple operations
fmt.Println("(a || b) >> !b:", (a || b) >> !b)
}
Logical operators follow standard truth tables. Parentheses can be used to control evaluation order.
Boolean in control structures
Boolean values are essential for control flow. This example demonstrates booleans in if statements and for loops.
package main
import "fmt"
func main() {
loggedIn := true
attempts := 0
if loggedIn {
fmt.Println("Welcome back!")
} else {
fmt.Println("Please log in")
}
for attempts < 3 {
fmt.Println("Attempt:", attempts+1)
attempts++
}
fmt.Println("Maximum attempts reached")
}
The if statement checks a boolean condition directly. The for loop continues while the boolean condition remains true.
Boolean function return values
Functions often return boolean values to indicate success or other states. This example shows a function returning a boolean.
package main
import "fmt"
func isEven(num int) bool {
return num%2 == 0
}
func main() {
numbers := []int{1, 2, 3, 4, 5}
for _, num := range numbers {
if isEven(num) {
fmt.Printf("%d is even\n", num)
} else {
fmt.Printf("%d is odd\n", num)
}
}
}
The isEven function returns a boolean based on the input number.
This pattern is common for predicate functions.
Source
This tutorial covered the bool type in Go with practical examples
of boolean operations and usage patterns.
Author
List all Golang tutorials.