Conditional if else statement in Go (Examples)
This tutorials shows examples and syntaxes for if
and else
statements in Golang.
Golang Conditional 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 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 the Syntax for if statements in Golang
if(Conditional Expression) {
// Code Block
}
Conditional Expression is evaluated and always results in 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 parentheses,
But the 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 the console. Below is an example of a simple if-conditional expression.
package main
import "fmt"
func main() {
var numerVariable = 10
if numerVariable%2 == 0 {
fmt.Printf("The event Number test passed ")
}
}
The output of the above code:
The 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 the 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 combine 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 the 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 a 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("The number is greater than 1000")
}
}
The output of the above program is
The 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.