Conditional Statements in Golang

Conditional constructs check the truth of a condition and, depending on the results of the test, allow you to direct the course of the program along one of the paths.

If Condition

The if construct accepts a condition, which is an expression that returns a value of type bool. If this condition is true, then the following set of instructions is executed.

package main
import (
	"fmt"
)

func main() {
	x := 10
	if x < 100 {
		fmt.Println("X is less than 100")
	}
}

The condition is placed after the if statement. In this case, it is checked whether the value of the variable x is less than the value 100. The value of variable x is indeed less than the value 100, that is, if the condition returns true, the subsequent block of code that prints the message to the console will be executed.

If we run this will get the message:

X is less than 100

If you want to specify alternative logic that is executed, if the condition is incorrect, then an else statement is added:

package main
import (
	"fmt"
)

func main() {
	x := 10
	if x < 100 {
		fmt.Println("X is less than 100")
	}else {
		fmt.Println("X is is not less than 100")
	}
}

Thus, if the conditional expression after the if statement is true, then the block after the if is true, if false, the block after else is executed.
If you want to check for more than one alternative, you can add else if statements:

package main

import (
	"fmt"
)

func main() {
	x := 10
	if x < 100 {
		fmt.Println("X is less than 100")
	}else if x == 100 {
		fmt.Println("X is equals to 100")

	}else{
		fmt.Println("X is grather than 100")
	}
}

Here,if the expression after the if is true, then the if block is executed. Otherwise, the statement after else if is checked. If it is true, then else if block. If it is false, the else block is executed.

Switch

A switch construct checks the value of some expression. Case statements are used to define values for comparison. If the value after the case statement matches the value of the switch expression, the code for that case block is executed.

package main

import (
	"fmt"
)

func main() {
	x := 10
	switch x {
	case 11:
		fmt.Println("x is equals to 9")
	case 10:
		fmt.Println("x is equals to 10")
	case 30:
		fmt.Println("x is equals to 100")
	}
}

The switch construct uses the variable a as an expression, and its value is sequentially compared with the values after the case statements. As variable x is equal to 10, then the block will be executed. The rest of the case blocks are not executed.

At the same time, after the switch statement, we can specify any expression that returns a value. For example:

package main

import (
	"fmt"
)

func main() {
	x := 10
	switch x-5 {
	case 11:
		fmt.Println("x is equals to 9")
	case 5:
		fmt.Println("x is equals to 5")
	case 30:
		fmt.Println("x is equals to 100")
	}
}

Here x is equal to 5 then the block "fmt.Println("x is equals to 5")" will be executed.

Also, the switch construct can contain an optional default block that is executed if none of the case statements does not contain the desired value:

package main

import (
	"fmt"
)

func main() {
	x := 10
	switch x-5 {
	case 11:
		fmt.Println("x is equals to 9")
	case 5:
		fmt.Println("x is equals to 5")
	case 30:
		fmt.Println("x is equals to 100")
	default:
		fmt.Println("there is no value here equals to x")
	}
}