The Boolean data type in Go is declared using the bool
keyword. It represents two values:
true
false
The default value of a Boolean variable is false
.
Example: Declaring Boolean Variables
This example demonstrates various ways to declare Boolean variables:
Go
x
package main
import ("fmt")
func main() {
var hasMotorcycleLicense bool = true // Typed declaration with initial value
var wearsHelmet = true // Untyped declaration with initial value
var isInsured bool // Typed declaration without initial value
isReadyToRide := true // Untyped declaration with shorthand syntax
fmt.Println(hasMotorcycleLicense) // Outputs true
fmt.Println(wearsHelmet) // Outputs true
fmt.Println(isInsured) // Outputs false
fmt.Println(isReadyToRide) // Outputs true
}
Output:
Go
true
true
false
true