Latest web development tutorials

Go language if ... else statement

Go language conditional statements Go language conditional statements

You can use the optional else statement if statement, else statement is false expression performs Boolean expressions.

grammar

Go programming language if ... else statement syntax is as follows:

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

If true, the statement block followed immediately by the execution in a boolean expression, else if false statement block is executed.

Flow chart is as follows:

Examples

package main

import "fmt"

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

}

The above code is executed as a result of:

a 不小于 20
a 的值为 : 100

Go language conditional statements Go language conditional statements