Learn Go: Conditionals
If you want to learn about implementing conditionals in Go, you've come to the right place!
StartKey Concepts
Review core concepts you need to learn to master this subject
Go If Statement
Go else Statement
Go Comparison Operators
Go Logical Operators
Go Else If Statement
Go Short Variable Declaration
Go Switch Statement
Go Seed Value
Go If Statement
Go If Statement
A Go if
statement evaluates a condition and executes a statement block enclosed in curly braces {..}
if the evaluation returns true
. The condition may be optionally enclosed inside a pair of parentheses (...)
.
if (healthy) { fmt.Println("Work.") } if sick { fmt.Println("Stay home.") }
Learn Go: Conditionals
Lesson 1 of 1
- 1We make decisions every day based on certain conditions. Is the alarm ringing? If so, turn it off. Is it raining? If so, bring an umbrella. Is the ice cream truck parked outside? If so, it’s time …
- 2What if…? What if we’re hungry? If it’s raining? If the alarm’s ringing? We would do something in response to these conditions. if statements work very similarly to our own decision-making proce…
- 3If we’re hungry we would go to eat something. But if we’re not hungry then we don’t. The idea is that we have a backup plan or something we can default to in case our condition isn’t met. We can p…
- 4So far we’ve been checking on boolean values (variable assigned a true or false value). But, we can check more than a single value using comparison operators. Here are two commonly used compariso…
- 5There are more comparison operators that we haven’t covered and they may seem familiar from math class: Operator | Meaning: — | — | Greater than = | Greater than or equal to Like the previ…
- 6In the previous exercises we checked one condition at a time. But what if we wanted to check multiple conditions at a time? To do so, we can use logical operators. There are three logical operato…
- 7Our last logical operator is the not (!) operator. It negates (reverses) the value of a boolean. For example: bored := true fmt.Println(!bored) // Prints false tired := false; fmt.Println(!tir…
- 8We can add different conditions to our if…else statements using an else if statement. Adding an else if statement allows us to check another condition after our if statement checks its condition….
- 9else if statements are great for checking multiple conditions. However, we can find ourselves needing to check so many conditions that writing all the necessary else if statements can feel tedious….
- 10We can also include a short variable declaration before we provide a condition in either if or switch statements. Here’s the syntax: x := 8 y := 9 if product := x * y; product > 60 { fmt.Println…
- 11Previously, we used hard coded values (values that don’t change) and then created conditionals that checked on these values. For example: alarmRinging := true if alarmRinging { fmt.Println(“…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory