Golang basic: Condition
Tags: go, tutorial
Published: 2020-12-11 16:49:54

When you are in a crossroad and you choose to turn right and you see the pizza restaurant. that is condition and what your decision and that you get.

Let's we write how go write their code
## Write Condition: if
Our decision is to turn right and we see the pizza restaurant.
``` golang
package main

import "fmt"

func main() {

  turn := "right"

  if turn == "right" {
    fmt.Println("Pizza restuarant")
  }
}

```
we use `if` to make condition if the value is true then the block `{...}` will be execute.

`turn == "right"` will return bool type `true` or `false`

``` golang
package main

import "fmt"

func main() {

  if true {
    fmt.Println("Always True")
  }
}
```
## Write Condition: else
Okay it just turn right but in the other side we see school how we go there. we could use `else` 

``` golang
package main

import "fmt"

func main() {

  turn := "left"

  if turn == "right" {
    fmt.Println("Pizza restaurant")
  } else {
    fmt.Println("School")
  }
}
```

It will check is `turn` equal with `"right"` if not it will go to `else` and execute block `else {...}` 

`else` have to write as `} else {` or you will get error compiler
``` golang
package main

import "fmt"

func main() {

  turn := "right"

  // It will get error compiler
  if turn == "right" {
    fmt.Println("Pizza restaurant")
  } 
  else {
    fmt.Println("School")
  }
}
``` 
## Write Condition: elseif
But wait no it is not other side, I want more specific what if I turn somewhere.
``` golang
package main

import "fmt"

func main() {

  turn := "left"

  if turn == "right" {
    fmt.Println("Pizza restaurant")
  } else if(turn == "left") {
    fmt.Println("Gas station")
  } else {
    fmt.Println("School")
  }
}
```
There more operator logic you can use on `if` condition 

| Operator | Mean |
| --------- |---- |
| == | equal |
| != | not equal |
| ! | not |
| && | and |
| < | less than|
| > | greater than|
| <= | less than or equal |
| >= | greater than or equal |

Is the data type just `string` value, NO. you can give condition with any data type
``` golang
package main

import "fmt"

func main() {

  if 17 < 20 {
    fmt.Print("17 is less than 20")
  } else {
    fmt.Print("17 is greater than 20")
  }
}
```
## Write condition: Switch
But is more complex imagine you look at street sign and what you can see? yeah the direction of the road.
you can convert `if` condition we have wrote before to `switch` condition
``` golang
package main

import "fmt"

func main() {

  turn := "right"

  switch(turn) {
  case "right" :
    fmt.Println("Pizza restaurant")
  case "left" :
    fmt.Println("School")
  default: 
    fmt.Println("Gas station")
  }
}
``` 
`switch` is a function that can write condition with a variable the example use `turn` variable, and the condition actually use `==` equal.

`case` will check is value of `turn` variable is equal to "right". and if it not equal then will check the `case "left"`.

if all conditions were not equal, so `default` will run.

don't forget to add `:` at the end of `case` or you will get error compiler.


## Conclusion 
We have created condition with two different functions and when we use `if` condition and when we use `switch` condition is up to you, but it look at the complexity and the logic in that condition.

`if` condition, you can give more logic ` !, !=, == , &&, ||` but in `switch` you cannot use them. 



Question: 
Write these line in `if` and `switch` conditions
if your age is 17 you have to get milk shake license 
if not you can't get milk shake license