Chapter 3

Making Decisions

IN THIS CHAPTER

Bullet Making decisions with the if/else statement

Bullet Evaluating multiple conditions with the switch statement

To make a program really useful, you need to be able to make decisions based on the values of variables and constants. Go offers a few constructs for making decisions:

  • If/else statements
  • Switch statements
  • Select statements

The third construct — the select statement — is for channel communication. I cover that subject in Chapter 12, where I explain more about channels.

In this chapter, I explain how to make decisions in Go using the if/else and switch statements.

Using If/Else Statements to Make Decisions

The first method of making decisions in Go is the if/else statement. An if/else statement basically says, “Do x if such-and-such is true; otherwise, do y.” In the following sections, I walk you through how to use the if/else statement, starting with the foundation of decision making: logical and comparison operators.

Laying the foundation for the if/else statement: Logical and comparison operators

Before I get to the if/else statement, though, you need to know a little bit about how programming works. In Go, a Boolean (bool) variable can take on a value of true or false. This seemingly simple concept is the cornerstone of programming. ...

Get Go Programming Language For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.