bool in Go



In this tutorial, we will learn about the boolean data type in Go. We will cover the basics of defining and using boolean values, including how to perform logical operations and use booleans in control structures.


Understanding the Boolean Data Type in Go

The bool data type in Go represents boolean values, which can be either true or false. Booleans are commonly used in control structures and logical operations.


Defining a Boolean Variable

Boolean variables in Go can be defined using the var keyword or by type inference.

var isActive bool = true
isClosed := false

Using Booleans in Control Structures

Boolean values are often used in control structures such as if statements to determine the flow of a program.

if isActive {
    fmt.Println("The system is active.")
} else {
    fmt.Println("The system is inactive.")
}

Logical Operations

Go supports standard logical operations such as AND, OR, and NOT on boolean values.

var a, b bool = true, false
fmt.Println(a && b) // AND operation
fmt.Println(a || b) // OR operation
fmt.Println(!a)     // NOT operation


Example 1: Defining and using Boolean Variables

We can define and use boolean variables in Go to represent true or false values.

For example,

  1. Define a boolean variable named isActive and assign it a value of true.
  2. Define another boolean variable named isClosed using type inference and assign it a value of false.
  3. Print the values of both boolean variables to the console.

Go Program

package main
import "fmt"
func main() {
    var isActive bool = true
    isClosed := false
    fmt.Println(isActive)
    fmt.Println(isClosed)
}

Output

true
false


Example 2: Using Booleans in Control Structures

We can use boolean values in control structures such as if statements to determine the flow of a program.

For example,

  1. Define a boolean variable named isActive and assign it a value.
  2. Use an if-else statement to print a message based on the value of isActive.

Go Program

package main
import "fmt"
func main() {
    isActive := true
    if isActive {
        fmt.Println("The system is active.")
    } else {
        fmt.Println("The system is inactive.")
    }
}

Output

The system is active.


Example 3: Performing Logical Operations

We can perform standard logical operations such as AND, OR, and NOT on boolean values in Go.

For example,

  1. Define two boolean variables named a and b with values true and false, respectively.
  2. Perform logical AND, OR, and NOT operations on these variables and print the results to the console.

Go Program

package main
import "fmt"
func main() {
    var a, b bool = true, false
    fmt.Println(a && b) // AND operation
    fmt.Println(a || b) // OR operation
    fmt.Println(!a)     // NOT operation
}

Output

false
true
false


Example 4: Combining Boolean Expressions

We can combine multiple boolean expressions using logical operators to create complex conditions.

For example,

  1. Define three boolean variables named x, y, and z with different values.
  2. Combine these variables in a logical expression using AND and OR operators and print the result to the console.

Go Program

package main
import "fmt"
func main() {
    x := true
    y := false
    z := true
    result := (x && y) || z
    fmt.Println(result) // prints the result of the combined boolean expression
}

Output

true