Introduction to Go Variables and Booleans

An exciting lesson awaits us, promising a deeper exploration of Go's variables and Boolean types. In earlier lessons, we covered the basics of Go, and now we're going to build on that by exploring how to use Boolean variables. These are simple yet powerful, used to represent a condition or status as either true or false.

What You'll Learn

In this lesson, we'll define two string variables: each will hold the name of a destination. We'll also explore how to use Boolean variables — a type of variable that can only be true or false, similar to a light switch that can only be either on (true) or off (false). Here's what it looks like in Go:

Here, destinationA and destinationB store strings representing travel destinations. On the other hand, hasVisitedA and hasVisitedB are Boolean variables that, like an on-off switch, inform us whether these destinations have been visited.

Getting Boolean Values from Expressions

In Go, Boolean values can be derived from various expressions using comparison operators. These operators compare values and return a Boolean result (true or false). Common comparison operators include:

  • == for equality
  • != for inequality
  • < for less than
  • <= for less than or equal to
  • > for greater than
  • >= for greater than or equal to

Here's how you can use these operators in Go:

These expressions are essential for making decisions in programming, enabling you to direct the flow of your program based on conditions.

Why It's Important

Understanding variables in Go is an essential skill for any programmer. They enable us to store, retrieve, and manipulate values within our code efficiently. Booleans, meanwhile, allow us to control program flow and make decisions based on conditions. Together, variables and booleans form a powerful foundation for writing dynamic and flexible programs.

Well done on reaching this stage in your Go journey! Everyone here on the course team is proud of your progress and can't wait to see what you achieve next.

Onward to our practice exercises!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal