Menu Close

Go – if else statement in go

In this article, We are going to learn about if else statement or conditional statement in go golang

Golang has following given below conditional statements or if else statement:

  • if statement
  •  if…else statement
  • if…else if…else statement

Golang – if statement 

if the specified condition is true then statements within if condition will be executed.

Syntax:

// it will be executed if condition is true
if (boolean_expression) {
     Statements 
 } 
package main

import "fmt"

func main() {
    var i = 100

    if (i == 100) {

        fmt.Println(i)

    }
}

Output:

100

Golang – if…else statement

In if…else statement, Only one block of code will be executed. If the condition is true then if condition block will be executed otherwise else part will be executed. 

Syntax:

if (boolean_expression) {
 
     Statements 

 } else {
 
     Statements 
} 
package main

import "fmt"

func main() {
    var i = 100

    if (i == 100) {

        fmt.Println("Condition is true")

    } else {

        fmt.Println("Condition is False")

    }
}

Output:

Condition is true
package main

import "fmt"

func main() {
    var i = 50

    if (i == 100) {

        fmt.Println("Condition is true")

    } else {

        fmt.Println("Condition is False")

    }
}

Output:

Condition is False

Golang – if…else if…else statement

If “if” condition is true then statements within “if” block will be executed . when this condition is false then it will check under else-if condition. If else-if condition is true, then it’s block will be executed otherwise else part will be executed. 

Syntax:

if  (boolean_expression_1) { 
 
     Statements
    
 } else if (boolean_expression_2) {
 
     Statements
     
 } else {
 
     Statements
     
 } 
package main

import "fmt"

func main() {
    var i = 100

    if (i == 100) {

        fmt.Println("Condition_1 is true")

    } else if (i == 50){

        fmt.Println("Condition_2 is true")

    } else {

        fmt.Println("Condition_3 is true")
    }
}
Condition_1 is true
package main

import "fmt"

func main() {
    var i = 50

    if (i == 100) {

        fmt.Println("Condition_1 is true")

    } else if (i == 50){

        fmt.Println("Condition_2 is true")

    } else {

        fmt.Println("Condition_3 is true")
    }
}

Output:

Condition_2 is true
package main

import "fmt"

func main() {
    var i = 20

    if (i == 100) {

        fmt.Println("Condition_1 is true")

    } else if (i == 50){

        fmt.Println("Condition_2 is true")

    } else {

        fmt.Println("Condition_3 is true")
    }
}

Output:

Condition_3 is true

variable initialization with if condition:

The if statement supports composite syntax where initialized variable to be tested for true or false.

Syntax:

if var declaration; condition {
     Statements
 } 
package main

import "fmt"

func main() {

    // i is initialized and is also tested for true and false condition
    if i := 20; (i == 20) {

        fmt.Println("Hello golang")

    }
}

Output:

Hello golang

Ternary operator ( ? ) 

Go does not support ternary operator (?).

In C language, we use ternary operator like given below example:

result = expression ? A : B  

Same in golang:

 if boolean_condition {
     Result = A
 } else {
     Result = B
 } 

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in golang

Leave a Reply

Your email address will not be published. Required fields are marked *