Skip to main content

Booleans

The bool type has exactly two values: true and false. It is the type returned by comparison and logical operators, and is the required condition type for if statements and loops.

Declaring Booleans

var b1 bool = true
var b2 bool = false
b3 := true // short declaration — type inferred as bool

Output:

b1: true | type: bool
b2: false | type: bool
b3: true | type: bool

Logical Operators

OperatorNameExampleResult
&&ANDtrue && falsefalse
||ORtrue || falsetrue
!NOT!truefalse
fmt.Println("true && false:", true && false) // false
fmt.Println("true || false:", true || false) // true
fmt.Println("!true:", !true) // false

Output:

true && false: false
true || false: true
!true: false

Key Takeaways

  • Only two values: a bool can only ever be true or false — Go has no truthy/falsy values
  • Type inference: := infers the type as bool when assigned true or false
  • Logical operators: &&, ||, and ! all return bool
  • if conditions: Go requires a bool expression — unlike Python or JavaScript, integers and other types are not implicitly boolean