0
0
GoConceptBeginner · 3 min read

What is bool in Go: Understanding Boolean Type in Go Language

bool in Go is a data type that represents a value of either true or false. It is used to store logical conditions and control the flow of a program.
⚙️

How It Works

Think of bool as a simple switch that can only be ON or OFF, represented by true or false. In Go, this type is used to hold these two states to help the program make decisions.

For example, when you want to check if a light is on or off, you use a bool variable. The program then uses this to decide what to do next, like turning the light on if it’s off.

This makes bool very useful for conditions, loops, and controlling how your program behaves based on true or false answers.

💻

Example

This example shows how to declare a bool variable and use it in an if statement to print a message based on its value.

go
package main

import "fmt"

func main() {
    var isRaining bool = true

    if isRaining {
        fmt.Println("Take an umbrella.")
    } else {
        fmt.Println("No umbrella needed.")
    }
}
Output
Take an umbrella.
🎯

When to Use

Use bool whenever you need to represent a yes/no or true/false condition in your program. It is perfect for checking if something is active, enabled, or meets a condition.

Common real-world uses include checking if a user is logged in, if a feature is turned on, or if a number meets a certain condition. This helps your program decide what actions to take next.

Key Points

  • bool holds only two values: true or false.
  • It is used to control program flow with conditions and loops.
  • Declaring a bool variable is simple and helps make decisions clear.
  • Default value of bool is false if not set.

Key Takeaways

bool represents true or false values in Go.
It is essential for making decisions in your code.
Use bool to check conditions like status or flags.
Default bool value is false when uninitialized.