Go Boolean Data Type
Boolean Data Type
A boolean data type is declared with thebool keyword and can only take the valuestrue orfalse.
The default value of a boolean data type isfalse.
Example
This example shows some 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
}
Try it Yourself »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
}
Note: Boolean values are mostly used for conditional testing which you will learn more about in theGo Conditions chapter.

