TExcelTAD
Home › Go › Go Booleans

Go Booleans

A boolean holds one of two values: true or false. Go's boolean type is called bool, and its zero value is false. Booleans are the foundation of every decision a program makes: if statements, loop conditions and filters all evaluate a bool.

go · try it yourself
package main

import "fmt"

func main() {
	var isOpen bool = true
	isAdmin := false
	var verified bool // zero value

	fmt.Println(isOpen, isAdmin, verified)
	fmt.Printf("%t %T\n", isOpen, isOpen)
}
true false false
true bool

Booleans from comparisons

Most booleans are not written literally — they come from comparison operators:

Operator Meaning 5 ? 3
== equal false
!= not equal true
< less than false
<= less than or equal false
> greater than true
>= greater than or equal true
go · try it yourself
package main

import "fmt"

func main() {
	age := 17
	stock := 0
	name := "Zoe"

	canVote := age >= 18
	soldOut := stock == 0

	fmt.Println("Can vote:", canVote)
	fmt.Println("Sold out:", soldOut)
	fmt.Println("Is Zoe:", name == "Zoe")
}
Can vote: false
Sold out: true
Is Zoe: true

Logical operators

Combine booleans with and (&&), or (||) and not (!).

a b a && b a || b !a
true true true true false
true false false true false
false true false true true
false false false false true
go · try it yourself
package main

import "fmt"

func main() {
	hasTicket := true
	isVIP := false
	age := 20

	fmt.Println("Enter:", hasTicket && age >= 18)
	fmt.Println("Backstage:", isVIP || age > 65)
	fmt.Println("Needs ticket:", !hasTicket)
}
Enter: true
Backstage: false
Needs ticket: false

Short-circuit evaluation

&& and || stop as soon as the result is known. With a && b, if a is false, b is never evaluated. This is useful for guarding against errors, such as indexing an empty slice:

go · try it yourself
package main

import "fmt"

func check(label string, result bool) bool {
	fmt.Println("evaluating", label)
	return result
}

func main() {
	items := []string{}

	// items[0] is never touched because len(items) > 0 is false
	if len(items) > 0 && items[0] == "x" {
		fmt.Println("found")
	}

	fmt.Println(check("A", false) && check("B", true))
	fmt.Println(check("C", true) || check("D", true))
}
evaluating A
false
evaluating C
true

Booleans are not numbers

In C or Python you can use 1 and 0 (or empty strings) as truth values. Go does not allow that — a condition must be a real bool.

go · try it yourself
package main

import "fmt"

func main() {
	count := 1
	if count {
		fmt.Println("non-zero")
	}
}
# command-line-arguments
./example.go:7:5: non-boolean condition in if statement

Write if count != 0 instead. To convert a bool to text, use strconv.FormatBool(b) or fmt.Sprint(b); to parse one, strconv.ParseBool("true").

Tip: Name boolean variables like yes/no questions: isEmpty, hasError, canRetry. Code like if hasError then reads like English.

Test yourself

What is the zero value of a bool?

  1. true
  2. false
  3. nil
  4. 0

In f() && g(), when is g() NOT called?

  1. When f() returns true
  2. When f() returns false
  3. Always called
  4. Never called

Is if 1 { ... } valid Go?

  1. Yes, 1 means true
  2. No, conditions must be of type bool
  3. Only inside loops
  4. Only with //go:allow