Go > Core Go Basics > Fundamental Data Types > Booleans (bool)
Boolean Declaration and Basic Operations in Go
This snippet demonstrates how to declare boolean variables in Go, assign values, and perform basic boolean operations like AND, OR, and NOT.
Boolean Declaration and Initialization
This section illustrates various ways to declare and initialize boolean variables in Go. We can declare a variable without an initial value, in which case it defaults to `false`. We can also assign a value during declaration or later. The `:=` short variable declaration allows Go to infer the type from the assigned value.
package main
import "fmt"
func main() {
// Declare a boolean variable with default value (false)
var isValid bool
fmt.Println("Initial value of isValid:", isValid)
// Assign a boolean value
isValid = true
fmt.Println("isValid after assignment:", isValid)
// Declare and initialize in one line
var isLoggedIn = false
fmt.Println("isLoggedIn:", isLoggedIn)
// Short variable declaration (type inference)
isAdmin := true
fmt.Println("isAdmin:", isAdmin)
}
Boolean Operators: AND, OR, NOT
This part demonstrates the fundamental boolean operators: AND (`&&`), OR (`||`), and NOT (`!`). The AND operator returns `true` only if both operands are `true`. The OR operator returns `true` if at least one operand is `true`. The NOT operator inverts the boolean value.
package main
import "fmt"
func main() {
// Boolean operators
a := true
b := false
// AND operator (&&)
resultAnd := a && b
fmt.Println("a && b:", resultAnd)
// OR operator (||)
resultOr := a || b
fmt.Println("a || b:", resultOr)
// NOT operator (!)
resultNotA := !a
fmt.Println("!a:", resultNotA)
resultNotB := !b
fmt.Println("!b:", resultNotB)
}
Concepts Behind the Snippet
Booleans represent truth values, either `true` or `false`. They are fundamental to control flow, allowing programs to make decisions based on conditions. Go's boolean type is distinct, meaning you can't directly convert integers to booleans like in some other languages (e.g., C). This helps prevent errors.
Real-Life Use Case
Booleans are extensively used in conditional statements (if/else), loops (for, while), and error handling. For example, checking if a user is authenticated (`isAuthenticated bool`), if a file exists (`fileExists bool`), or if an operation was successful (`success bool`). They're also used to control the execution of features based on configurations or user roles.
Best Practices
Interview Tip
Be prepared to explain the different boolean operators and their truth tables. Understand how short-circuiting works in boolean expressions (e.g., in `a && b`, if `a` is `false`, `b` is not evaluated). Also, be aware of the importance of booleans in control flow and error handling.
When to Use Them
Use booleans whenever you need to represent a binary state: yes/no, on/off, true/false. They are essential for creating logic that responds to different situations and conditions within your code.
Memory Footprint
In Go, a boolean variable typically occupies 1 byte of memory. This makes them very efficient for storing and manipulating truth values.
Alternatives
While booleans are the standard way to represent truth values, you could technically use integers (0 for false, 1 for true) or strings ("true", "false"), but this is strongly discouraged. Using booleans directly provides type safety and clarity. Avoid creating custom boolean types unless there is a strong domain specific reason.
Pros
Cons
FAQ
-
What is the default value of a boolean variable in Go?
The default value of a boolean variable in Go is `false`. -
Can I convert an integer to a boolean in Go?
No, Go does not allow direct conversion of integers to booleans. You must use explicit comparison operators (e.g., `if x != 0 { ... }`) to derive a boolean value from an integer. -
Are boolean operators short-circuited in Go?
Yes, Go's boolean operators `&&` and `||` are short-circuited. This means that if the result of the expression can be determined from the left-hand operand, the right-hand operand is not evaluated. For example, in `a && b`, if `a` is `false`, `b` is never evaluated.