Boolean Data Type in Go
- In Go, the boolean data type is declared using the bool keyword. It can hold only one of two values: true or false.
- Default value: If a boolean variable is declared without an initial value, it defaults to false.
Example:
- The following example demonstrates several ways to declare boolean variables:
package main
import "fmt"
func main() {
var b1 bool = true // Explicit type with initial value
var b2 = true // Implicit type with initial value
var b3 bool // Explicit type without initial value (defaults to false)
b4 := true // Short declaration with initial value
fmt.Println(b1) // Output: true
fmt.Println(b2) // Output: true
fmt.Println(b3) // Output: false
fmt.Println(b4) // Output: true
}
Note: Boolean values are commonly used in conditional expressions, which you'll explore further in the Go Conditions section.