Go Conditions

  • date 19th August, 2022 |
  • by Prwatech |
  • 0 Comments

Go supports the usual comparison operators :

  1. Less than <
  2. Less than or equal <=
  3. Gretter then >
  4. Gretter than or equal >=
  5. Equal to ==
  6. Not Equal to !=
  7. Logical AND &&
  8. Logical OR | |
  9. Logical NOT !
  • If Statement

If the statement is used to specify a block of Go Code to be executed if a condition is True.

Syntax:

            If  condition {

                        // conditions

}

In the example below, we test two values to find out if 200 is greater than 180. If the condition is true, print some text

package main

import "fmt"
    
func main() {
    x := 200
    y := 180
    if x > y {
        fmt.Println("x is greater than y")
    }
}

Output :

PS C:\GO_Language\conditions> go run if.go
x is greater than y
  • The else Statement
Syntax:
    If condition {
        // executed if the condition is false
    }else {
        // executed if the condition is false
    }
  • Using The if-else Statement
  1. In this example, time (20) is greater than 18, so the if condition is false. Because of this, we move on to the else condition and print to the screen “Good evening”. If the time was less than 18, the program would print “Good day”:
    package main
    import "fmt"
    
    func main() {
      time := 20
      if (time < 18) {
        fmt.Println("Good day.")
      } else {
        fmt.Println("Good evening.")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifelse.go
    Good evening.
    
  2. In this example, the temperature is 14 so the condition for if is false so the code line inside the else statement is executed:
    package main
    import ("fmt")
    
    func main() {
      temperature := 14
      if (temperature > 15) {
        fmt.Println("It is warm out there")
      } else {
        fmt.Println("It is cold out there")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifelse2.go
    It is cold out there
    
  3. Having the else brackets in a different line will raise an error:
    package main
    import "fmt"
    
    func main() {
      temperature := 14
      if (temperature > 15) {
        fmt.Println("It is warm out there.")
      } 			// this raises an error
      else {
        fmt.Println("It is cold out there.")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifelse3.go
    # command-line-arguments
    .\ifelse3.go:10:3: syntax error: unexpected else, expecting }
    
    • else if Statement

    else if statement is used to specify a new condition if the first condition is false.

    Syntax:
    if condition1 {
       // executed if condition1 is true
    } else if condition2 {
       // executed if condition1 is false and condition2 is true
    } else {
       // executed if condition1 and condition2 are both false
    }
    
  4. This program shows how to use an else if
    package main
    import "fmt"
    
    func main() {
      time := 22
      if time < 10 {
        fmt.Println("Good morning.")
      } else if time < 20 {
        fmt.Println("Good day.")
      } else {
        fmt.Println("Good evening.")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifils4.go
    Good evening.
    

     

  5. Another example for the use of else if.
    package main
    import "fmt"
    
    func main() {
      a := 14
      b := 14
      if a < b {
    
        fmt.Println("a is less than b.")
      } else if a > b {
        fmt.Println("a is more than b.")
    go run
      } else {
        fmt.Println("a and b are equal.")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifelse5.go
    a and b are equal.
    

     

  6. If conditions1 and condition2 are both correct, only the code for condition1 is executed:
    package main
    import "fmt"
    
    func main() {
      x := 30
      if x >= 10 {
        fmt.Println("x is larger than or equal to 10.")
      } else if x > 20
        fmt.Println("x is larger than 20.")
      } else {
        fmt.Println("x is less than 10.")
      }
    }
    

    Output :

    PS C:\GO_Language\conditions> go run ifelse6.go
    x is larger than or equal to 10.
    

     

0
0

Quick Support

image image