Boolean Data Type #
A boolean data type in Go is declared with the bool
keyword and can only hold two possible values: true
or false
.
- The default value of a boolean variable is
false
.
Example #
This example demonstrates different ways to declare boolean variables:
package main
import ("fmt")
func main() {
var b1 bool = true // typed declaration with initial value
var b2 = true // untyped declaration with initial value
var b3 bool // typed declaration without initial value
b4 := true // untyped declaration with initial value
fmt.Println(b1) // Returns true
fmt.Println(b2) // Returns true
fmt.Println(b3) // Returns false
fmt.Println(b4) // Returns true
}
Output: #
true
true
false
true
Note: Boolean values are mostly used for conditional testing, which you will learn more about in the Go Conditions chapter.