Nesting if statements in Go

Go language conditional statement

You can embed one or more if or else if statements within an if or else if statement.

Syntax

The syntax of the if...else statement in the Go programming language is as follows:

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

You can nest within if statements in the same way.else if...elsestatement

Example

Using nested if statements:

Example

package main

import "fmt"

func main() {
   /* Define local variables */
   var a int = 100
   var b int = 200
 
   /* Check condition */
   if a == 100 {
       /* Execute if condition is true */
       if b == 200 {
          /* Execute if condition is true */
          fmt.Printf(The value of a is 100, and the value of b is 200\n" );
       }
   }
   fmt.Printf(a value is : %d\n", a );
   fmt.Printf(b value is : %d\n", b );
}

The execution result of the above code is:

a 的值为 100 , b 的值为 200
a 值为 : 100
b 值为 : 200

Go language conditional statement

other extensions