Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The bool type in Go is a predeclared, strictly typed primitive representing a boolean truth value. It is restricted to exactly one of two universe block identifiers: true or false.

Memory and Architecture

A bool in Go occupies exactly 1 byte (8 bits) of memory, regardless of the underlying system architecture (32-bit or 64-bit).

Zero Value

The zero value for an uninitialized bool is false.
var b bool
// b is strictly evaluated as false

Strict Type Safety

Go enforces strict type boundaries for booleans. Unlike C or Python, Go does not treat numeric values (like 0 or 1) or nil pointers as truthy or falsy. A bool cannot be implicitly or explicitly converted to or from any numeric type.
var b bool = true

// INVALID: Cannot convert bool to int
// var i int = int(b) 

// INVALID: Cannot evaluate integer as boolean
// if 1 { ... } 

Operators and Evaluation

The bool type supports logical and equality operators. Go employs short-circuit evaluation for logical operators, meaning the right-hand operand is only evaluated if the left-hand operand does not definitively determine the overall expression’s result.
  • Logical AND (&&): Yields true if both operands are true. Short-circuits if the left operand is false.
  • Logical OR (||): Yields true if either operand is true. Short-circuits if the left operand is true.
  • Logical NOT (!): Unary operator that negates the boolean value.
  • Equality (==, !=): Compares two boolean values.
var x bool = true
var y bool = false

resAnd := x && y // false
resOr  := x || y // true
resNot := !x     // false
resEq  := x == y // false

String Formatting

When interacting with the fmt package, the specific format verb for a bool is %t.
package main

import "fmt"

func main() {
    var isActive bool = true
    fmt.Printf("Status: %t\n", isActive) // Output: Status: true
}

Type Definition

When creating a new defined type based on bool, the new type inherits the memory footprint and boolean characteristics but becomes a distinct type. It loses implicit type compatibility with the predeclared bool and requires explicit conversion. This differs from a true type alias (type FeatureFlag = bool), which would remain completely interchangeable.
type FeatureFlag bool // Type definition, creating a distinct type

var isEnabled FeatureFlag = true
var rawBool bool = false

// INVALID: Mismatched types FeatureFlag and bool
// rawBool = isEnabled 

// VALID: Explicit conversion
rawBool = bool(isEnabled)
Master Go with Deep Grasping Methodology!Learn More