Go Language if Statement
Go Language Conditional Statements
The if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax for an if statement in Go programming language is as follows:
if boolean expression {
/* Executed when the boolean expression is true */
}
If the boolean expression evaluates to true, the block of statements following it is executed. If it evaluates to false, the block is not executed.
Here is the flowchart:
Example
Using if to determine the size of a variable:
Example
package main
import "fmt"
func main() {
/* Define local variables */
var a int = 10
/* Use if statement to evaluate boolean expression */
if a < 20 {
/* Statements to be executed if the condition is true */
fmt.Printf("a is less than 20\n")
}
fmt.Printf("The value of a is: %d\n", a)
}
The output of the above code is:
a is less than 20
The value of a is: 10