Lesson 7
Navigating Go's Conditional Cosmos: Steering Through if-else and Beyond
Charting Our Coding Trajectory: Overview of Conditional Statements in Go

Greetings, Go astronaut in training! Today's itinerary includes studying the mainstay of programming control flow: conditional statements. These mechanisms steer the course of our Go program. Are you strapped in and ready to explore the if-else statement? Let's start the countdown now!

Mapping the If and If-Else Constellation

The structure of if and if-else control flow in Go reflects the following:

Go
1if condition { 2 // action if condition is true 3} 4 5// additionally 6 7if condition { 8 // action if condition is true 9} else { 10 // action if condition is false 11}

Here, when the given condition becomes true, we take action via the if block. When the condition is false, we have an optional else block to resort to.

Probing the Nebula of Go's If-Else Statement

By using the if statement in Go, we command the machine to undertake specific actions only when conditions are met. Let's imagine deciding to land on a planet with breathable air:

Go
1var oxygenLevel = 78 // The level of oxygen on the planet 2 3if oxygenLevel > 20 { 4 fmt.Println("Planet has breathable air!") // Oxygen level is suitable 5} else { 6 fmt.Println("Oxygen level too low!") // Oxygen level is not high enough 7} 8// The code prints: Planet has breathable air!

In the example, the statement if oxygenLevel > 20 tests if the oxygen level is greater than 20. If the test passes (true), it prints: "Planet has breathable air!". If it fails (false), the else clause provides an alternative command and prints: "Oxygen level too low!".

Multiple Conditions: The Else If Statement

When dealing with multiple conditions, we fall back on else if:

Go
1var oxygenLevel = 58 2if oxygenLevel > 70 { 3 fmt.Println("Excellent Oxygen level!") 4} else if oxygenLevel > 50 { 5 fmt.Println("Oxygen level is acceptable.") 6} else { 7 fmt.Println("Oxygen level is too low!") 8} 9// The code prints: Oxygen level is acceptable.

With the else if keyword, we can map out alternative routes until we find a fitting one, which allows us to adapt suitably to different levels of oxygen. As soon as one condition is met, the program disregards subsequent else if conditions.

Navigating Through Interstellar Switches

In Go, a switch statement provides a way of checking multiple conditions in a concise and readable format. It's similar to if, else if, else, but more structured.

Here is the basic format of a switch:

Go
1switch condition { 2 case condition_1: 3 // do something if condition_1 4 case condition_2: 5 // do something if condition_2 6 default: 7 // do something if none of the conditions are met 8}

The switch tests a condition, and executes the block of code associated with the first case clause that is true. If no case condition is fulfilled, the default clause is executed.

Switch Example

Let’s consider an example:

Go
1var planet = "Mars" 2 3switch planet { 4 case "Earth": 5 fmt.Println("Planet is Earth.") 6 case "Mars": 7 fmt.Println("Planet is Mars.") 8 default: 9 fmt.Println("Unidentified Planet.") 10} 11// The code prints: Planet is Mars.

Here, we examine the value of the planet variable. The switch tests each case invoking the first condition that matches the planet value. In this situation, it triggers "Planet is Mars.". If no match is found, the default clause executes, but in this case it is not required.

Multiple Values in Switch

In Go, a case statement can have multiple comma-separated values. Let's consider an example:

Go
1 food := "apple" 2 3 switch food { 4 case "apple", "banana", "orange": 5 fmt.Println(food, "is a fruit.") 6 case "carrot", "broccoli", "radish": 7 fmt.Println(food, "is a vegetable.") 8 case "chicken", "beef", "fish": 9 fmt.Println(food, "is a meat.") 10 default: 11 fmt.Println("Unknown food category.") 12 } 13 14 // Output: apple is a fruit

If any of the values listed in the case statement matches our target variable, the code inside this case statement is executed.

Revising Our Stellar Course Before Liftoff

Well done! You've successfully navigated through the intricate landscape of Go's conditional statements today. Reinforce your understanding with our subsequent exercises. Each completed task furthers your mastery of the expanse of Go, preparing you for the next stage of your coding exploration. Fasten your harness, and we'll see you in the next class!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.