In Go, a boolean is a data type that can hold only one of two values: true or false. Booleans are primarily used to test conditions or represent the truth state of a value.
Here's an example of declaring and initializing a boolean variable in Go:
var isSunny bool = true
In this example, the variable isSunny is declared and initialized to the value true - indicating that it's sunny outside.
We can use boolean variables to make decisions in code using conditionals. For example:
if isSunny {
fmt.Println("It's sunny!")
} else {
fmt.Println("It's not sunny.")
}
This code prints "It's sunny!" if isSunny is true, and "It's not sunny." if isSunny is false.
We can also use boolean operators to combine multiple boolean values or negate them. Here are some common boolean operators in Go:
- && (and) - returns true if both operands are true, otherwise returns false
- || (or) - returns true if at least one of the operands is true, otherwise returns false
- ! (not) - negates (flips) the value of the operand (i.e. if it was true, it becomes false and vice versa)
Here's an example that uses boolean operators to check if it's both sunny and hot outside:
var isHot bool = true
if isSunny && isHot {
fmt.Println("It's hot and sunny!")
} else {
fmt.Println("It's not that hot or sunny.")
}
This code prints "It's hot and sunny!" if isSunny and isHot are both true, and "It's not that hot or sunny." otherwise.
Overall, booleans are a simple yet powerful tool for representing the truth state of values and making conditional decisions in Go.