Conditional statements allow you to execute decisions based on logic that evaluates to true or false. Some examples of conditional statements include if statements, if…else, if…else if.. else, and switch-case statements. Go has full support for these conditionals with a familiar and straightforward syntax.

Getting Started With Golang

To follow along with this tutorial, install Go in your local environment if you haven't already. You can also test the code snippets in this article using the Go playground.

If Statement

The if statement executes a block of code only if a certain condition is met. Here is the syntax:

if condition {
// Block of code
}

Here is an example that prints “Pass” if the value of the marks variable is more than 50:

marks := 60

if marks > 50 {
fmt.Println("Pass")
}

You could also declare the marks variable in the if statement like this:

if marks := 60; marks > 50 {
fmt.Println("Pass")
}

When combining conditions, Go allows you to use logical operators familiar from other languages like AND(&&), OR (||), and NOT(!).

The AND operator returns true only if the value on its right and left are true:

sunny := true
noClass := true

if sunny && noClass {
fmt.Println("Go to the beach")
}

The OR operator returns true if one of the values either on the right or left is true:

today := "Sunday"

if today == "Sunday" || today == "Saturday" {
fmt.Println("Sleep in")
}

The NOT operator returns true only if the value is false:

marks := true

if !marks {
fmt.Println("No marks available!")
}

If…Else Statement

The if…else statement executes the corresponding code depending on whether the condition is met.

if condition {
// Executes if condition is met
} else {
// Executes if condition is not met
}

In the example below, the program prints ‘Pass’ if the total marks value is above 50 and ‘Fail’ if it’s below.

marks := 60

if marks > 50 {
fmt.Println("Pass")
} else {
fmt.Println("Fail")
}

If…Else If…Else Statement

The if…else if…else statement allows you to combine multiple if statements.

if condition1 {
// Executess if condition1 is met
} else if condition2 {
// Executess if condition2 is met
} else {
// Executes if neither condition1 nor condition2 is met
}

Extending the if…else example, the program below also checks whether the marks are above 80 and prints “Passed with distinction” if so:

marks := 60

if marks > 80 {
fmt.Println("Passed with distinction")
} else if marks > 50 {
fmt.Println("Pass")
} else {
fmt.Println("Fail")
}

Switch Statement

The switch statement is a conditional statement that allows you to execute different actions based on the value of an expression. You can identify significant values as “cases” and act on them accordingly.

The switch statement in Go is slightly different from in other programming languages like C#, JavaScript, and Python. This is because it only executes the block of code under the met case. The break keyword, required by other languages, isn't necessary here:

switch expression {
case expr1:
// Block of code
case expr2:
// Block of code
default:
// Block of code
}

The above code evaluates the expression after the switch keyword, then compares it to each case value. If a value matches, the following block runs. You can have as many cases as you need, but they must all be unique. The default block runs if there is no match.

The following example uses a switch statement to display a “todo” item for each day of the week.

package main

import (
"fmt"
"time"
)

func main() {
today := time.Now().Weekday()

switch today {
case 0:
fmt.Println("Relax.")
case 1:
fmt.Println("Clean the house.")
case 2:
fmt.Println("Visit the dentist.")
case 3:
fmt.Println("Weed the garden.")
case 4:
fmt.Println("Send gifts")
case 5:
fmt.Println("Do laundry.")
case 6:
fmt.Println("Write a blog post.")
default:
fmt.Println("No task scheduled.")
}
}

Here, Go’s time package provides the day of the week as an integer and, depending on that value, the switch statement prints a certain task.

When to Use Conditional Statements

Conditional statements help you create a decision flow in your program. Like many other languages, Go supports several types of conditional statement. You can use them to streamline your program and ensure its logical operation is correct.

Use conditional statements when the flow of your program depends on a certain value such as user input.