Hello!

The topic of today’s post is If and Switch statements in go.

## If#

The if block is executed if the condition returns true.

``````package main

import "fmt"

func main() {
if true {
fmt.Println("Condition is true")
}

if false {
fmt.Println("Condition is false")
}
}
``````
``````\$ go run main.go
Condition is true
``````

In this case, only `Conditions is true` was printed because the condition in the second if a block was false. Let’s check an example that is closer to real life.

``````package main

import "fmt"

func main() {
firstNumber := 10
secondNumber := 20

if firstNumber > secondNumber {
fmt.Println("First number is bigger")
}

if firstNumber < secondNumber {
fmt.Println("First number is bigger")
}

if firstNumber == secondNumber {
fmt.Println("First number is bigger")
}
}
``````
``````\$ go run main.go
First number is bigger
``````

Here, again, only the first line was displayed. Because all others returned false because the first number is greater than the second. If you change the numbers, a different line will be printed.

In addition to the above < less than, > greater than and == equal to, golang has the following operators != not equal, >= greater or equal, and <= less or equal.

Also, in one if block there can be several conditions that can be combined with && - and or || - or. && will be true only if all conditions return true. At || will return true if one of the conditions returns true.

``````package main

import "fmt"

func main() {
firstNumber := 10
secondNumber := 20

if firstNumber >= 20 && secondNumber >= 20 {
fmt.Println("Both number higher then 20")
}

if firstNumber >= 20 || secondNumber >= 20 {
fmt.Println("One or both numbers higher then 20")
}

if firstNumber != secondNumber {
fmt.Println("Numbers are not equal")
}
}
``````
``````\$ go run main.go
One or both numbers higher then 20
Numbers are not equal
``````

In this example, the second message is displayed because one of the numbers is greater than 20. Also, the last message is displayed because the numbers are not equal. In order for not all conditions to be evaluated, but for example, to stop after the first one that returned true, you can use if/else. Let’s rewrite the same code with the addition of else.

``````package main

import "fmt"

func main() {
firstNumber := 10
secondNumber := 20

if firstNumber >= 20 && secondNumber >= 20 {
fmt.Println("Both number higher then 20")
} else if firstNumber >= 20 || secondNumber >= 20 {
fmt.Println("One or both numbers higher then 20")
} else if firstNumber != secondNumber {
fmt.Println("Numbers are not equal")
} else {
fmt.Println("Final case")
}
}
``````
``````\$ go run main.go
One or both numbers higher then 20
``````

In this case, when the first condition returned true, the next ones are no longer evaluated, so we see only one output.

## Switch#

A switch is similar to the if condition but is used when we have many similar checks.

``````package main

import "fmt"

func main() {
number := 30

switch number {
case 10:
fmt.Println("Number is 10")
case 20:
fmt.Println("Number is 20")
case 30:
fmt.Println("Number is 30")
default:
fmt.Println("Default cast")
}
}
``````

Several cases can be combined into one

``````package main

import "fmt"

func main() {
number := 30

switch number {
case 10, 20, 30:
fmt.Println("Number is 10, 20 or 30")
case 40, 50, 60:
fmt.Println("Number is 40, 50 or 60")
default:
fmt.Println("Default cast")
}
}
``````
``````\$ go run main.go
Number is 10, 20 or 30
``````

As in if, you can use comparison operators. The switch looks the same, only the name of the parameter we are checking is not indicated next to it.

``````package main

import "fmt"

func main() {
number := 30

switch {
case number <= 30:
fmt.Println("Number is less then 30")
case number <= 60:
fmt.Println("Number is less then 60")
default:
fmt.Println("Default cast")
}
}
``````
``````\$ go run main.go
Number is less than 30
``````

In this case, the next case where `number <= 60` is not executed, although it also returns true, for it to be called, you need to add a keyword `fallthrough`, but then the next case will be executed regardless of the condition in it

``````package main

import "fmt"

func main() {
number := 30

switch {
case number <= 30:
fmt.Println("Number is less then 30")
fallthrough
case number <= 10:
fmt.Println("Number is less then 10")
default:
fmt.Println("Default cast")
}
}
``````
``````\$ go run main.go
Number is less than 30
Number is less than 10
``````

Even though `number <= 10` returned false, `Number is less than 10` was still printed.

Another option for using cases is type-checking.

``````package main

import "fmt"

func main() {
var n any = 1

switch n.(type) {
case int:
fmt.Println("N is int")
case string:
fmt.Println("N is string")
default:
fmt.Println("Undefined type")
}
}
``````
``````\$ go run main.go
N is int
``````

If you replace 1 with “1”, `N is string` will be displayed.