{

Learn Golang Tutorials - if else statement examples


Learn control flow structures - if-else statements in Go Language with examples.

Learn Golang Tutorials - if else statement examples

golang if statements

Like many programming languages, Go Language provides control structures for decision-making. if and else statements are one of type. if statements use to execute a piece of code based on the conditional expressions. if the condition is satisfied, the code block is executed.

if and else in golang are reserved words.

This blog post covers examples and syntaxes for the following things.

  • simple if statements
  • if shorthand statements
  • if-else statements
  • if-else nested statements

Here is Syntax for if statements in Golang

if(Conditional Expression) {  
Code Block  
}  

Conditional Expression is evaluated and always results intrue or false.

The expression can be made of simple boolean values or complex expressions created using comparison operators ==,= or !=.

the expression can be single or multiple.

Multiple expressions are joined with logical operators like &&,||,! operators. Conditional Expressions are not required to be enclosed with parenthesis,

But code block must be enclosed with curly braces. In golang, curly braces are required for code block which contains sing statements.

simple if statement in Golang

This example evaluates the expression if the expression is true, the code block is executed and returns “Event Number test passed " to console. The below is an example for simple if the conditional expression.

package main  
  
import "fmt"  
  
func main() {  
    var numerVariable = 10  
    if numerVariable%2 == 0 {  
        fmt.Printf("Event Number test passed ")  
    }  
}  

the output of the above code:

Event Number test passed 

Let’s see an error case in the if-else block example.

Below the example, the code gives a compilation error.

The reason is if statements code block has no curly braces, It throws syntax error: unexpected newline, expecting { after if clause.

package main  
  
import "fmt"  
  
func main() {  
    var numerVariable = 10  
    if numerVariable %2 == 0   
        fmt.Printf("Event Number test passed ")  
}  

Multiple expressions combines with logical operators.

var number = 2  
if  number >= 1 && age <= 10 {  
    fmt.Println("Number is between 1 to 10")  
} 

if with shorthand statements syntax in Golang

if statement always contains the shorthand statements, before going control to the conditional expression.

shorthand statements can contain variable declaration and the scope of the variable is limited to the scope of this block only.

func main() {  
    var numerVariable = 10  
    if result := numerVariable % 2; result == 0 {  
        fmt.Printf("Event Number test passed ")  
    }  
}  

if you include shorthand syntax in if statements, the parenthesis is not required.

It gives compilation error - syntax error: unexpected:=, expecting ), if parenthesis is in shorthand syntax.

func main() {  
    var numerVariable = 10  
    if (result := numerVariable % 2; result == 0) {  
        fmt.Printf("Event Number test passed ")  
    }  
}  

if-else statement in Golang

This syntax contains an else block for the if statement. if the conditional expression is true if the block is executed, otherwise else block is executed.

if (conditional expression){  
//if true this block of code is executed  
}else{  
//if false block of code is executed  
}  

example for the if-else statement

package main  
  
import "fmt"  
  
func main() {  
    var numerVariable = 10  
    if numerVariable%2 == 0 {  
        fmt.Printf("Event Number if block ")  
    } else {  
        fmt.Printf("Odd Number else block")  
  
    }  
}  

the output of the above program is

Event Number if block  

multiple if-else - nested if statements in Golang

if the statement contains if-else statements that contain chains of if-else statements

package main  
  
import "fmt"  
  
func main() {  
    var numerVariable = 1001  
    if numerVariable <= 100 {  
        fmt.Printf("Number is less than 100")  
    } else if numerVariable > 100 && numerVariable <= 1000 {  
        fmt.Printf("Number is greater than 100 and less than 1000")  
    } else { // else statements  
        fmt.Printf("Number is greater than 1000")  
    }  
}  

The output of the above program is

Number is greater than 1000  

Compilation error if any line breaks before else statement as like below example the below program gives syntax error: unexpected else, expecting }

func main() {  
    var numerVariable = 1001  
    if numerVariable <= 100 {  
        fmt.Printf("Number is less than 100")  
    } else if numerVariable > 100 && numerVariable <= 1000 {  
        fmt.Printf("Number is greater than 100 and less than 1000")  
    }   
    else { // else statements  
        fmt.Printf("Number is greater than 1000")  
    }  
}  

Conclusion

In this tutorial, You learned conditional flow if-else blocks in Golang for example.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.