Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Go if Statement

Go if Statement

Go Language Conditional Statements Go Language Conditional Statements

An if statement consists of a boolean expression followed by one or more statements.

Syntax

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



if 布尔表达式 {

   /* execute when the boolean expression is true */

}

If the boolean expression is true, the block immediately following it executes; if it is false, nothing executes.

The flowchart is as follows:

Example

Use if to judge the magnitude of a numeric variable:

Example

package

main

import

"fmt"

func

main

()

{

   

/* Define local variables */

   

var

a

int

=

10

 

   

/* Use an if statement to judge a boolean expression */

   

if

a <

20

{

       

/* If the condition is true, execute the following statements */

       fmt

.

Printf

(

"a is less than 20\n"

)

   

}

   fmt

.

Printf

(

"The value of a is : %d\n"

,

a

)

}

The execution result of the code above is:



a 小于 20

a 的值为 : 10

Go Language Conditional Statements Go Language Conditional Statements