Go Boolean

Summary: In this tutorial, you’ll learn about Go’s bool type, which helps programs represent true-or-false values, compare information, and combine simple conditions.

By the end of this tutorial, you’ll know:

  • What a boolean is
  • How true and false work
  • How to create and update boolean variables
  • What the default value of bool is
  • How comparisons produce boolean values
  • How to use the !, &&, and || operators
  • How to choose clear names for boolean variables
  • Common beginner mistakes to avoid

What Is a Boolean?

A boolean is a value that represents one of two possible states:

true

or:

false

You can think of them as:

  • true means yes, correct, or active
  • false means no, incorrect, or inactive

For example, imagine that a user is logged in.

We can represent that state with:

true

If the user is not logged in, we can represent that state with:

false

In Go, the type used to store true or false is called:

bool

Creating a Boolean Variable

A variable is a named place where a program stores a value. We can create a boolean variable like this:

var isLoggedIn bool = true

Let’s look at each part.

var

The var keyword tells Go that we want to create a variable.

isLoggedIn

This is the name of the variable.

The name describes the information stored in the variable:

Is the user logged in?

bool

This is the variable’s type.

It tells Go that the variable can store only true or false.

true

This is the value stored in the variable.

The complete line can be read as:

Create a boolean variable named isLoggedIn and give it the value true.

Printing a Boolean Value

Here is a complete Go program:

package main

import "fmt"

func main() {
    var isLoggedIn bool = true

    fmt.Println(isLoggedIn)
}

Output:

true

The following line prints the value stored in isLoggedIn:

fmt.Println(isLoggedIn)

Because the value of isLoggedIn is true, the program prints true.

Boolean Values Are Lowercase

In Go, boolean values must be written in lowercase:

true
false

These are incorrect:

True
False
TRUE
FALSE

Go treats uppercase and lowercase letters differently. Always use lowercase letters for boolean values.

Creating a Boolean with false

A boolean variable does not have to start as true. It can also start as false:

var isLoggedIn bool = false

Complete example:

package main

import "fmt"

func main() {
    var isLoggedIn bool = false

    fmt.Println(isLoggedIn)
}

Output:

false

The value tells us that the user is not logged in.

The Default Value of bool

What happens if we create a boolean variable without giving it a value?

var isLoggedIn bool

Go automatically gives the variable a starting value of false.

package main

import "fmt"

func main() {
    var isLoggedIn bool

    fmt.Println(isLoggedIn)
}

Output:

false

This automatic starting value is called the zero value.

The zero value is the value that Go gives to a variable when we do not provide one. For the bool type, the zero value is always false.

package main

import "fmt"

func main() {
    var isLoggedIn bool
    var isAccountActive bool
    var isPaymentComplete bool

    fmt.Println(isLoggedIn)
    fmt.Println(isAccountActive)
    fmt.Println(isPaymentComplete)
}

Output:

false
false
false

None of the variables received a value, so all of them started as false.

Changing a Boolean Value

A variable’s value can change while the program is running.

package main

import "fmt"

func main() {
    var isLoggedIn bool = false

    fmt.Println(isLoggedIn)

    isLoggedIn = true

    fmt.Println(isLoggedIn)
}

Output:

false
true

At first, isLoggedIn contains false.

The following line changes its value:

isLoggedIn = true

Notice that we do not write var again. The variable already exists, so we are only changing its value.

Using a Short Variable Declaration

Go provides a shorter way to create a variable:

isLoggedIn := true

The := symbol creates a new variable and gives it a value.

Go sees the value true and understands that isLoggedIn must be a bool.

These two lines have a similar result:

var isLoggedIn bool = true
isLoggedIn := true

The shorter form is commonly used inside functions:

package main

import "fmt"

func main() {
    isLoggedIn := true

    fmt.Println(isLoggedIn)
}

Output:

true

Be careful with these symbols:

:= // Create a new variable
=  // Change the value of an existing variable

Example:

isLoggedIn := false
isLoggedIn = true

The first line creates the variable. The second line changes its value.

Comparisons Produce Boolean Values

A comparison checks the relationship between two values.

For example:

Is 10 greater than 5?

In Go, we write:

10 > 5

The answer is yes, so the result is true.

package main

import "fmt"

func main() {
    fmt.Println(10 > 5)
}

Output:

true

Here is another comparison:

10 < 5

This asks whether 10 is less than 5. The answer is no, so the result is false.

A comparison always produces a boolean value: true or false.

Boolean Comparison Operators

An operator is a symbol that tells Go to perform an action.

Go provides several operators for comparing values.

Equal To: ==

The == operator checks whether two values are equal.

fmt.Println(10 == 10)
fmt.Println(10 == 20)

Output:

true
false

Not Equal To: !=

The != operator checks whether two values are different.

fmt.Println(10 != 20)
fmt.Println(10 != 10)

Output:

true
false

Greater Than: >

fmt.Println(10 > 5)

Output:

true

Less Than: <

fmt.Println(10 < 5)

Output:

false

Greater Than or Equal To: >=

fmt.Println(10 >= 10)

Output:

true

Less Than or Equal To: <=

fmt.Println(7 <= 10)

Output:

true

Assignment and Comparison Are Different

The symbols = and == have different meanings.

A single equal sign assigns a value:

isLoggedIn = true

A double equal sign compares two values:

isLoggedIn == true

Example:

package main

import "fmt"

func main() {
    isLoggedIn := true

    fmt.Println(isLoggedIn == true)
    fmt.Println(isLoggedIn == false)
}

Output:

true
false

Remember:

=  // Assign a value
== // Compare two values

Storing a Comparison Result

Because a comparison produces true or false, we can store its result in a boolean variable.

var isAdult bool = 20 >= 18

Go evaluates the right side first. The comparison 20 >= 18 produces true, and Go stores that result in isAdult.

package main

import "fmt"

func main() {
    age := 20
    isAdult := age >= 18

    fmt.Println(isAdult)
}

Output:

true

If we change age to 15, the comparison produces false:

package main

import "fmt"

func main() {
    age := 15
    isAdult := age >= 18

    fmt.Println(isAdult)
}

Output:

false

Comparing Text

We can also compare text values.

A text value in Go is written inside double quotation marks:

"Go"

We can check whether two text values are equal or different:

fmt.Println("Go" == "Go")
fmt.Println("Go" != "Python")
fmt.Println("Go" == "go")

Output:

true
true
false

Go treats uppercase and lowercase text as different. "Go" and "go" are not equal.

Reversing a Boolean with !

Sometimes we want the opposite of a boolean value.

Go uses the ! operator for this. The ! operator means NOT.

!true  // false
!false // true

Example:

package main

import "fmt"

func main() {
    isLoggedIn := false

    fmt.Println(isLoggedIn)
    fmt.Println(!isLoggedIn)
}

Output:

false
true

The original value is false. Its opposite is true.

Combining Boolean Values with &&

Sometimes we need to combine two boolean values.

Imagine that a user is logged in and has an active account:

isLoggedIn := true
isAccountActive := true

The && operator means AND:

isLoggedIn && isAccountActive

The result is true only when both values are true.

package main

import "fmt"

func main() {
    isLoggedIn := true
    isAccountActive := true

    canAccess := isLoggedIn && isAccountActive

    fmt.Println(canAccess)
}

Output:

true

The complete && behavior is:

false && false -> false
false && true  -> false
true  && false -> false
true  && true  -> true

With AND, every condition must be true.

Combining Boolean Values with ||

The || operator means OR.

It produces true when at least one boolean value is true.

package main

import "fmt"

func main() {
    isAdmin := false
    isOwner := true

    canManageAccount := isAdmin || isOwner

    fmt.Println(canManageAccount)
}

Output:

true

isAdmin is false, but isOwner is true. At least one value is true, so the final result is true.

The complete || behavior is:

false || false -> false
false || true  -> true
true  || false -> true
true  || true  -> true

With OR, at least one condition must be true.

Combining ! and &&

Let’s combine what we have learned.

A user can access an app when the user is logged in and is not blocked.

package main

import "fmt"

func main() {
    isLoggedIn := true
    isBlocked := false

    canAccess := isLoggedIn && !isBlocked

    fmt.Println(canAccess)
}

Output:

true

Let’s evaluate it step by step:

  • isLoggedIn is true.
  • isBlocked is false.
  • !isBlocked is true.
  • The full expression becomes true && true.
  • The final result is true.

Numbers Are Not Booleans in Go

In some programming languages, the number 0 may represent false, and another number may represent true.

Go does not work that way.

This is a number:

0

This is a boolean:

false

They are different types of values.

If we want to produce a boolean from a number, we must make a clear comparison:

package main

import "fmt"

func main() {
    count := 0
    hasItems := count > 0

    fmt.Println(hasItems)
}

Output:

false

The expression count > 0 asks whether count is greater than zero.

Choosing Good Boolean Variable Names

A good variable name should clearly describe what true means.

Boolean variable names often begin with words such as:

  • is
  • has
  • can
  • should

Examples:

isLoggedIn := true
isAccountActive := false
hasPermission := true
hasDiscount := false
canEdit := true
shouldRetry := false

These names sound like yes-or-no questions:

  • Is the user logged in?
  • Is the account active?
  • Does the user have permission?
  • Does the customer have a discount?
  • Can the user edit?
  • Should the program retry?

Avoid unclear names such as:

value := true
status := false
flag := true
check := false

These names do not explain what true or false means.

Practice

Exercise: Predict the Default Value

What will this program print?

package main

import "fmt"

func main() {
    var isReady bool

    fmt.Println(isReady)
}

Answer

false

A boolean variable without an assigned value starts as false.

Exercise: Compare Values

Predict the output:

package main

import "fmt"

func main() {
    fmt.Println(5 > 3)
    fmt.Println(10 == 8)
    fmt.Println(7 != 7)
}

Answer

true
false
false

Explanation:

  • 5 > 3 is true.
  • 10 == 8 is false.
  • 7 != 7 is false because both values are equal.

Exercise: Reverse a Boolean

Predict the output:

package main

import "fmt"

func main() {
    isBlocked := false

    fmt.Println(isBlocked)
    fmt.Println(!isBlocked)
}

Answer

false
true

The ! operator reverses false to true.

Exercise: Combine Boolean Values

Predict the output:

package main

import "fmt"

func main() {
    hasTicket := true
    isEventOpen := false

    fmt.Println(hasTicket && isEventOpen)
    fmt.Println(hasTicket || isEventOpen)
}

Answer

false
true

Explanation:

  • true && false is false because both values are not true.
  • true || false is true because at least one value is true.

Exercise: Create an isAdult Variable

Create a program that:

  • Creates an age variable with the value 20
  • Compares age with 18
  • Stores the result in a variable named isAdult
  • Prints isAdult

Example Solution

package main

import "fmt"

func main() {
    age := 20
    isAdult := age >= 18

    fmt.Println(isAdult)
}

Output:

true

Summary

  • A boolean represents one of two values: true or false.
  • The Go type for boolean values is bool.
  • The zero value of bool is false.
  • Boolean values must be written in lowercase.
  • The = operator assigns a value, while == compares two values.
  • Comparison operators such as ==, !=, >, <, >=, and <= produce boolean values.
  • The ! operator reverses a boolean value.
  • The && operator produces true only when both values are true.
  • The || operator produces true when at least one value is true.
  • Numbers are not boolean values in Go. Use a comparison such as count > 0 to produce a boolean.
  • Clear names such as isLoggedIn, hasPermission, and canEdit make boolean values easier to understand.