Latest web development tutorials

Go language if statement

Go language conditional statements Go language conditional statements

if statement is followed by a Boolean expression after one or more statements.

grammar

Go programming language syntax if the statement is as follows:

if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
}

If true, the statement block followed immediately by the execution in the Boolean expression is false if not executed.

Flow chart is as follows:

Examples

package main

import "fmt"

func main() {
   /* 定义局部变量 */
   var a int = 10
 
   /* 使用 if 语句判断布尔表达式 */
   if a < 20 {
       /* 如果条件为 true 则执行以下语句 */
       fmt.Printf("a 小于 20\n" )
   }
   fmt.Printf("a 的值为 : %d\n", a)
}

The above code is executed as a result of:

a 小于 20
a 的值为 : 10

Go language conditional statements Go language conditional statements