Conditional Statements in Go

Conditional statements help control what code runs and what doesn’t depending on whether a certain condition or conditions are met.

Boolean Variables

Boolean values are either true or false and can be assigned to variables like any other value type.

The code example below shows how you can create boolean variables using the long and shorthand declaration methods.

package main

import "fmt"

func main() {
	var flavorful bool = true
	expensive := false
	fmt.Println(flavorful)
	fmt.Println(expensive)
}

Boolean Operators

Boolean operators, also called logical operators, compare conditional statements and return True or False.

Operator Description
&& Logical AND. Returns True if both statements are True
|| Logical OR. Returns True if either statement is True
! Logical NOT. Returns True if statement is False

The example code below shows how to print the result of boolean operation on two values.

package main

import "fmt"

func main() {
	one := true
	two := false
	fmt.Println(one && two)
	fmt.Println(one || two)
	fmt.Println(!one)
}

The output is:

false
true
false

Relational Operators

Relational operators compare one value to another. The comparison returns a true or false boolean value. Combine them with boolean operators to make even more complex comparisons.

Operator Description
== Equal
!= Not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

The code snippet below demonstrates their usage.

package main

import "fmt"

func main() {
	x := 3
	y := 4
	fmt.Println(x > y)
	fmt.Println(x <= 3)
	fmt.Println(x+1 == y && y < 10)
}

The output is:

false
true
true

If Statements

An if statement evaluates a condition and then run a block of code it the statement is true.

The following example checks if the price variable is higher than 10 and if so, prints a message to the console.

package main

import "fmt"

func main() {
	price := 1000

	if price > 10 {
		fmt.Println("It is too expensive")
	}
}

The output is:

It is too expensive

The if-else Statement

The if-else statement is like an if-statement but if the statement is not true, the code in the else block run instead.

package main

import "fmt"

func main() {
	price := 1

	if price > 10 {
		fmt.Println("It's too expensive")
	} else {
		fmt.Println("We can afford it!")
	}
}

The output is:

We can afford it!

The else-if Statement

The else if statement can be used to evaluate more than one condition.

package main

import "fmt"

func main() {
	price := 101

	if price < 10 {
		fmt.Println("We can afford it!")
	} else if price > 100 {
		fmt.Println("That price is insane!")
	} else {
		fmt.Println("That price is too high.")
	}
}

The output is:

That price is insane!

More If Statements

We can include boolean operators in our if statements to check multiple conditions at once.

Review the code below. If the price is less than 10, we print our message. However, if the price is less than 100 AND a variable called is_birthday is true, then we also print the message.

if price < 10 || price < 100 && is_birthday {
	fmt.Println("I'm buying it!")
}

We can also nest if-statements inside one another. Look at the code snippet below. If the price is below 10, we enter the if-statement code block. We then check another if-statement to see if the price variable again. If the value is less than 4, we print the interior statement, but if it’s false, we print another.

if price < 10 {
	if price < 4 {
		fmt.Println("Wow! That's a great deal.")
	} else {
		fmt.Println("Sounds like a fair price.")
	}	
}

Summary

In this post we learned about booleans and how relational operators return their values when comparisons between two variables are made. We then explored if statements in Go and how they can determine what block of code gets executed.

In the next lesson we look at Loops in Go.