We can control the flow of the program depending on the values of variables or the boolean result of functions. Below is a simple if structure:

if condition-1 {
	// Code Block 1 - Executed if condition-1 is true
} else if condition-2 {
	// Code Block 2 - Executed if condition-2 is true
} else {
	// Code Block 3 - Executed if both condition-1 
    // and condition-2 are false
}

As can be seen from above condition-1 and condition-2 must be a boolean or result into a boolean. Let's see an example below:

package main

import (
	"fmt"
)

func main() {
	value := 10
	
	fmt.Println("Simple If Statement ------------")
	if value < 20 {
		fmt.Println("The value is less than 20")
	}
	
	fmt.Println("\nIf-Else-If Statement ------------")
	if value < 10 {
		fmt.Println("The value is less than 10")
	} else if value < 20 {
		fmt.Println("The value is less than twenty")
	}
	
	fmt.Println("\nIf-Else Statement ------------")
	if value < 10 {
		fmt.Println("The value is less than 10")
	} else {
		fmt.Println("The value is not less than 10")
	}
	
	fmt.Println("\nIf-Else-If-Else Statement ------------")
	if value < 10 {
		fmt.Println("The value is less than 10")
	} else if value > 20 {
		fmt.Println("The value is greater than 20")
	} else {
		fmt.Println("The value is in anywhere from 10 to 20")
	}
}

GoLang Playground Link

Result:

Simple If Statement ------------
The value is less than 20

If-Else-If Statement ------------
The value is less than twenty

If-Else Statement ------------
The value is not less than 10

If-Else-If-Else Statement ------------
The value is in anywhere from 10 to 20

Explanation

Simple If-Statement

value := 10
	
fmt.Println("Simple If Statement ------------")
if value < 20 {
	fmt.Println("The value is less than 20")
}

The code above checks whether the value is less than 20. If it is, then it executes the lines within the code block. Otherwise, it will skip these lines. In this instance, since 10 is less than 20, it will print "The value is less than 20".

If-Else-If Statement

value := 10

fmt.Println("\nIf-Else-If Statement ------------")
if value < 10 {
	fmt.Println("The value is less than 10")
} else if value < 20 {
	fmt.Println("The value is less than twenty")
}

In this example, we have if-else-if structure. It will first check the first condition. If the first condition (`value < 10`) is true, it will execute the code block within it and it won't check the next condition (`value < 20`). In the code above, the first condition is false, therefore, it checked the next condition which results to true. Therefore, it will print "The value is less than twenty".

In the event that all conditions are false, it will skip all the code blocks.

If-Else Statement

value := 10

fmt.Println("\nIf-Else Statement ------------")
if value < 10 {
	fmt.Println("The value is less than 10")
} else {
	fmt.Println("The value is not less than 10")
}

In the code above, we can see an else keyword which means that if all the conditions are false, this code block will be executed. In the example above, the first condition is false because value is not less than 10. Therefore, the else block is executed.

If-Else-If Statement

value := 10

fmt.Println("\nIf-Else-If-Else Statement ------------")
if value < 10 {
	fmt.Println("The value is less than 10")
} else if value > 20 {
	fmt.Println("The value is greater than 20")
} else {
	fmt.Println("The value is in anywhere from 10 to 20")
}

The code above is pretty similar to the previous examples. In this example, we have a value that is equal to 10. Since both the first two conditions resulted to false, the else code block is executed.

If, for example, value is 9. The first condition will be true and the program will print "The value is less than 10". Since the first condition was satisfied, the checking for the other conditions and the else block are skipped.


Multiple Conditions

It is also possible to have multiple conditions in an if statement. Below is an example:

package main

import (
	"fmt"
)

func main() {
	num := 4
	
	if num < 5 && num % 2 == 0 {
		fmt.Println("The number is less than 5 AND is even")
	}
	
	if num > 5 || num % 2 == 0 {
		fmt.Println("The number is greater than 5 OR is even")
	}
}

GoLang Playground Link

Result:

The number is less than 5 AND is even
The number is greater than 5 OR is even

Explanation

&& - The AND Operator

The && operator is true if both conditions are true. See the combinations below:

  • TRUE && TRUE => TRUE
  • TRUE && FALSE => FALSE
  • FALSE && TRUE => FALSE
  • FALSE && FALSE => FALSE
  • 4 < 5 && 6 < 7 => TRUE (both are true, therefore, the result is true)
  • 4==4 && 5 <4 => FALSE (4 is equal to 4 but 5 is not less than 4, therefore, the result is false)

If we use the AND operator, it is important to remember that ALL conditions MUST be true in order for it to result to true.

|| - The OR Operator

The || operator (two pipelines) is true if one or more conditions are true. See the combinations below:

  • TRUE || TRUE => TRUE
  • TRUE || FALSE => TRUE
  • FALSE || TRUE => TRUE
  • FALSE || FALSE => FALSE
  • 4 < 5 || 6 < 7 => TRUE (both are true, therefore, the result is true)
  • 4==4 || 5 <4 => FALSE (4 is equal to 4, therefore, the result is true even if 5 is not less than 4)

Using the OR operator, only ONE condition has to be true for it to be evaluated to true.


Function Result

Functions can return a boolean value. Therefore, we can call a function which returns a boolean and use it as a condition. See the example below:

package main

import (
	"fmt"
)

func main() {
	num := 3
	
	if isEven(num) {
		fmt.Println("The number is even")
	} else {
		fmt.Println("The number is odd")
	}
}

func isEven(num int) bool {
	return num % 2 == 0
}

GoLang Playground Link

Result:

The number is odd

Explanation

We will be covering more of what functions are in later sections. For the meantime, we can think of functions as a way to group our codes and call these codes repeatedly without duplicating them. Functions can return one or more result and can accept one or more parameters.

In the example above, our function isEven accepts one parameter num with a type of int and it returns a type of bool. The result will be true if num is even. Otherwise, the result will be false.

In this instance, num is 3 and it is odd. Therefore, the result is false and, so, our program executed the codes in the else block.