In this tutorial, we will take a look at different conditional statements like if, else if, else and switch statements.
If, else if, else statements
If you have ever used conditionals in different languages you might be familiar with this. Let’s start with the if statement.
goIsAwesome := true
if goIsAwesome {
fmt.Println("I love Go")
}
This will evaluate to true and the if statement will run and print “I love Go”. One difference between languages like Java, C is that there are no parentheses.
We can also expand if statements with else if and else.
language := "Go"
if language == "Go" {
fmt.Println("Keep rocking")
} else if language == "Python" {
fmt.Println("Solid choice")
} else {
fmt.Println("Exploring new languages?")
}
In case if
is false it will move on to else if
and see if it’s true. If all those statements are false it will call the else
statement.
If you have a variable that you want to be only available for the different conditions you can create it straight in the if
statement.
foods := []string{"Pizza", "Sushi", "Burger", "Tacos"}
if choice := foods[rand.Intn(len(foods))]; choice == "Pizza" {
fmt.Println("Pizza is delicious! Great choice.")
} else if choice == "Sushi" {
fmt.Println("Sushi is a healthy option. Nice!")
} else if choice == "Burger" {
fmt.Println("Burger is always a good comfort food.")
} else {
fmt.Println("You like", choice, "which is interesting!")
}
Here we create a slice with different foods, and we get a random food and save it to the choice variable. We print food sentences based on the random food. Notice the choice variable it’s declared in the if statement and it will be available for all related conditions. However, it will not be available outside.
Switch statement
Switch statements are a cleaner alternative for managing multiple conditions. If you have worked with other languages switch statements are very similar in those.
day := 6
switch day {
case 1:
fmt.Println("Sunday")
case 2:
fmt.Println("Monday")
case 3:
fmt.Println("Tuesday")
case 4:
fmt.Println("Wednesday")
case 5:
fmt.Println("Thursday")
case 6:
fmt.Println("Friday")
case 7:
fmt.Println("Saturday")
default:
fmt.Println("Invalid day")
}
First, we create a variable day with a value of 6. After that Go will check every case is it equal to the value of day else move on to the next one. In this case it will match case 6 and print out Friday and after that it will stop. Default will be trigged if no other cases is matched.
In other languages you have to add a break
statement after every case else it would fall through to the other cases. In Go you don’t need to do that it does it for you.
Switch statements can also trigger for multiple values under the same case.
fruit := "banana"
switch fruit {
case "apple", "cherry":
fmt.Println("Red")
case "banana", "lemon":
fmt.Println("Yellow")
case "grape", "plum":
fmt.Println("Purple")
default:
fmt.Println("Unknown color")
}
Here yellow will get printed since it matches the banana case.
You can also use something called blank switches. In the black switch you don’t specify what value you are comparing instead you do a check inside every case that returns a boolean
temperature := 15 // degrees Celsius
switch {
case temperature < 5:
fmt.Println("Winter")
case temperature >= 5 && temperature < 15:
fmt.Println("Autumn")
case temperature >= 15 && temperature < 25:
fmt.Println("Spring")
case temperature >= 25:
fmt.Println("Summer")
default:
fmt.Println("Unusual weather")
}
This will print out Spring since the temperature is equal to 15.
Conclusion
Full source code can be found here Github
In this tutorial, we explored Go’s if-else and switch statements. These tools help you with decision-making. In the next tutorial we are gonna cover loops