Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Types  ›  Lesson

Booleans

Boolean 10 minA bool is only true or false, conditions must be real booleans with no truthy numbers or strings, and && and || stop evaluating as soon as the answer is known.

Booleans

tally should print extra detail when the user adds -v. That is a yes-or-no question, and Go answers yes-or-no questions with exactly one type, bool. Go is stricter about it than most languages — and that strictness removes a whole family of bugs.

The rule

A condition is a bool — nothing else counts — and &&, || stop early.

A bool holds true or false, and its zero value is false. You get one from a literal, a comparison, or the logic operators:

verbose := false
long := len(word) > 7          // comparison: true or false
ok := long && !verbose         // and, not
either := long || verbose      // or

No truthiness. In some languages 0, "" or an empty list count as false. In Go they do not count at all:

if len(words) { }       // compile error: non-boolean condition
if len(words) > 0 { }   // fine

There is also no conversion between bool and numbers: int(true) does not compile. Say what you mean with a comparison.

Short-circuit. a && b does not evaluate b when a is false; a || b does not evaluate b when a is true. That makes guards possible:

verbose := len(os.Args) > 2 && os.Args[2] == "-v"

If there are only two arguments, the right side is never run, so os.Args[2] never panics.

Printing uses %t or plain Println, which shows true or false. Turning text into a bool uses strconv.ParseBool("true").

The shortcut

For every && or ||, put the cheap, safe check on the left and the risky one on the right. Then read left to right and stop the moment the answer is settled.

Evaluating tally's flag with 2 argumentsStep 1 of 4
  1. Left side

    len(os.Args) > 2 is 2 > 2: false.

Swap the sides and the same input panics before the length check ever runs.

Drills

Answer fast — trust the rule.

quick drill

What is the zero value of a bool?

quick drill

Which condition compiles?

quick drill

a := false. In a && expensive(), how many times does expensive run?

quick drill

What does fmt.Println(3 > 2 || 1/zero == 0) print, where zero is an int variable holding 0?

Remember

  • A condition is a bool — nothing else counts — and &&, || stop early.
  • Shortcut: cheap, safe check on the left; risky check on the right.
  • Trap: truthiness habits from other languages do not compile in Go.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe safe verbose flag

tally turns on verbose mode when the second argument is "-v". Explain why verbose := len(os.Args) > 2 && os.Args[2] == "-v" never crashes, why swapping the two sides could, and why if len(os.Args) { ... } does not compile.