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
| Operator | Name | Example | Result |
|---|---|---|---|
&& | AND | true && false | false |
|| | OR | true || false | true |
! | NOT | !true | false |
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
boolcan only ever betrueorfalse— Go has no truthy/falsy values - Type inference:
:=infers the type asboolwhen assignedtrueorfalse - Logical operators:
&&,||, and!all returnbool ifconditions: Go requires aboolexpression — unlike Python or JavaScript, integers and other types are not implicitly boolean
Related Topics
- Conditionals — using booleans in
ifandswitchstatements - Comparison Operators — operators that produce
boolvalues - Go Types — other built-in types