Key Concepts

Review core concepts you need to learn to master this subject

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.") }

Go else Statement

A Go else statement can succeed an if or if else-if statement block and its code executed if the conditions in the preceding if or if else-if statements evaluate to false.

sick := false if sick { fmt.Println("Call the doctor.") } else { fmt.Println("Enjoy your day.") }

Go Comparison Operators

Go supports the standard comparison operators that compare two values and return a boolean. These include:

  • == equivalence operator
  • != not equal
  • < less than
  • > greater than
  • <= less than or equal
  • >= greater than or equal
    same := 3 == 3 // evaluates to true notsame := "ABC" != "abc" // evaluates to true lessthan := 5 <= -5 // evaluates to false

Go Logical Operators

In addition to comparison operators, Go also supports logical operators which evaluate boolean values and return a boolean value. For example:

  • && is the AND operator that returns true if all the booleans are true
  • || is the OR operator that returns true if one of the booleans is true
  • ! is the NOT operator that returns the opposite of a boolean value
    answer := true && false // returns false answer = true || false // returns true answer = !false // returns true

Go Else If Statement

The Go else if statement provides an additional condition to evaluate besides the first if conditional. It can only appear after the if statement and before an else statement if it exists. For example:

if (temperature < 60) { fmt.Println("Put on a jacket.") } else if (temperature >= 60 && temperature < 75) { fmt.Println("Put on a light sweater.") } else { fmt.Println("Wear summer clothes.") }

Multiple else if statements can exist alongside the if statement. The if else if statements are scanned from top to bottom and only the code block associated with a true condition is executed. If none of the conditions are satisfied, the else code block is executed if it exists.

Go Short Variable Declaration

A short variable declaration can be made within the scope of an if or switch statement before the condition is specified but after the if or switch keyword. A semicolon, ;, is appended to the declaration to separate it from the condition.

if age := 55; age >= 55 { fmt.Println("You are retiring!") } switch season := "spring"; season { case "spring": fmt.Println("Plant some bulbs.") case "summer": ... }

Go Switch Statement

The Go switch statement can be used as an alternative to a set of if followed by else if statements. The switch statement compares the expression inside a condition with a set of values encapsulated in cases. The code inside a matched case value is executed and the switch block terminates. A default case without a value can be appended to the last case and its code executed if no prior match is found.

day := "Tuesday" switch day { case "Monday": fmt.Println("Monday is magnificent.") case "Tuesday": fmt.Println("Tuesday is terrific.") case "Wednesday": fmt.Println("Wednesday is wacky.") default: fmt.Println("We survived.") }

Go Seed Value

A seed value in Go is used for generating random numbers. By default, the seed value is 1 and this leads to a predictable number instead of random. To make the seed value unique, call the seed function, rand.Seed(), with the argument time.Now().UnixNano() to return the difference in time (in Nanoseconds) since Janurary 1st 1970.

rand.Seed(time.Now().UnixNano())

Go Random Number Generator

Go provides a function, math.rand.Intn(), in the math.rand package to generate a random number. To generate such a number between 0 to 99, pass 100 as the function argument.

number := math.rand.Intn(100)
Learn Go: Conditionals
Lesson 1 of 1
  1. 1
    We 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 …
  2. 2
    What 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…
  3. 3
    If 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…
  4. 4
    So 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…
  5. 5
    There 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…
  6. 6
    In 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…
  7. 7
    Our 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…
  8. 8
    We 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….
  9. 9
    else 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….
  10. 10
    We can also include a short variable declaration before we provide a condition in either if or switch statments. Here’s the syntax: x := 8 y := 9 if product := x * y; product > 60 { fmt.Println(…
  11. 11
    Previously, 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(“…
  12. 12
    Previously, we saw how our random numbers weren’t entirely random. The reason for this behavior is due to how Go seeds or chooses a number as a starting point for generating random numbers. By de…
  13. 13
    If you’ve made it to this exercise, then you’ve finished Go’s conditionals lesson, great job! Here are the topics covered In this lesson: * How to create an if statement that checks a condition an…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo