Go language if statement
Go language conditional statement
An if statement consists of a boolean expression followed by one or more statements.
Syntax
The syntax of an if statement in the Go programming language is as follows:
if 布尔表达式 {
/* 在布尔表达式为 true 时执行 */
}
If the boolean expression is true, the block of statements immediately following it is executed; if it is false, it is not executed.
The flowchart is as follows:
Example
Use if to compare the size of a numeric variable:
Example
package main
import "fmt"
func main() {
/* Define local variables */
var a int = 10
/* Use the if statement to evaluate a boolean expression */
if a < 20 {
/* if the condition is true, execute the following statement */
fmt.Printf("a is less than 20\n" )
}
fmt.Printf("The value of a is : %d\n", a)
}
import "fmt"
func main() {
/* Define local variables */
var a int = 10
/* Use the if statement to evaluate a boolean expression */
if a < 20 {
/* if the condition is true, execute the following statement */
fmt.Printf("a is less than 20\n" )
}
fmt.Printf("The value of a is : %d\n", a)
}
The execution result of the above code is:
a 小于 20 a 的值为 : 10other extensions