Go language "fifth": Go conditional statement

Source: Internet
Author: User
Tags define local

Go language conditions

Conditional statements require the developer to decide whether to execute the specified statement by specifying one or more conditions and to test whether the condition is true, and to execute additional statements if the condition is false. Shows the structure of conditional statements in a programming language:

The Go language provides the following conditional judgment statements:

Statement Description
If statement An If statement consists of a Boolean expression followed by one or more statements
If...else statements An If statement can use an optional else statement , and an expression in the Else statement executes when the Boolean expression is False
If nested statements You can embed one or more if or else if statements in an if or else if statement
Switch statement Switch statements are used to perform different actions based on different conditions
SELECT statement The Select statement is similar to a switch statement, but Select executes a running case randomly, and if there is not one, it will block until a case can be run

Go language if statement
The If statement consists of a Boolean expression followed by one or more statements with the following syntax:

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

If the Boolean expression is true, then the statement block immediately following it executes, and if False, does not execute. The process is as follows:

Instance code:

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

The result of the above code execution is:

20a 的值为:10

Go Language If...else statement
An If statement can use the optional Else statement, and the expression in the Else statement executes when the Boolean expression is false, with the syntax:

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

If the Boolean expression is true, the statement block immediately following it executes, and if False, executes the ELSE statement block, with the flowchart as follows:

Instance code:

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

The result of the above example operation:

20a 的值为:100

Go language if nested
You can nest one or more if or else if statements in an if or else if statement with the following syntax:

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

You can use the same method to nest the else if ... else statement in an IF statement
Instance code:

 PackageMainImport "FMT"funcMain () {/ * Define Local Variables * /    varAint= -    varBint= $    / * Judging condition * /    ifA = = -{/ * If condition statement is true * /        ifb = = ${/ * If condition statement is true * /Fmt. Printf ("A has a value of +, and B has a value of\ n")}} FMT. Printf ("A value is:%d\ n", a) fmt. Printf ("B value is:%d\ n", b)}

The above code runs the result:

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

Go Language switch statement
The switch statement is used to perform different actions based on different conditions, each of which is unique, from top to bottom, until a match is performed, and the switch statement executes from top to bottom until a match is found, and the following syntax is not required to add a break after the match:

switch varl {    case varl1:        ...    case val2:        ...    default:        ...}

Variable Varl can be of any type, while VARL1 and VARL2 can be any value of the same type. Types are not limited to constants or integers, but must be of the same type, or the final result is an expression of the same type. The process is as follows:

Instance code:

 PackageMainImport "FMT"funcMain () {/ * Define Local Variables * /    varGradestring="B"    varMarksint= -    SwitchMarks { Case  -: Grade ="A"     Case  the: Grade ="B"     Case  -, -, -: Grade ="C"    default: Grade ="D"}Switch{ CaseGrade = ="A": FMT. Printf ("Excellent!" \ n") CaseGrade = ="B", Grade = ="C": FMT. Printf ("Good!" \ n") CaseGrade = ="D": FMT. Printf ("Pass!" \ n") CaseGrade = ="F": FMT. Printf ("Less than a lattice!" \ n")default: FMT. Printf ("Poor!" \ n")} FMT. Printf ("Your rating is%s\ n", Grade)}

The result of the above code execution is:

优秀!你的等级是 A

Type switch
The switch statement can also be used for Type-switch to determine the type of variable actually stored in a interface variable, with the following syntax:

switch x.(type){    casetype:        statement(s)    casetype:        statement(s)    /* 可以定义任意个数的case */    default:    // 可选        statement(s)}

code example:

 PackageMainImport("FMT")funcMain () {varXInterface{}SwitchI: = x. (type){ Case Nil: FMT. Printf ("type of x:%T"I Case int: FMT. Printf ("x is type int") Case float64: FMT. Printf ("x is float64 type") Case func(int)float64: FMT. Printf ("X is a func (int) type") Case BOOL,string: FMT. Printf ("X is a bool or string type")default: FMT. Printf ("Unknown type")    }}

The result of the above code execution is:

x 的类型:<nil>

Go language SELECT statement
Select is a control structure in go, similar to the switch statement for communication, each case must be a communication operation, either sent or received. The select executes a running case randomly, and if there is not one, it will block until there is a case to run. A default clause should always be operational, with the following syntax:

select {    case communication clause:        statement(s)    case communication clause:        statement(s)    /* 可以定义任意数量的case */    default:    // 可选        statement(s)}

The syntax for the SELECT statement is described below:

    • Each case must be a communication
    • All channel expressions will be evaluated
    • All expressions that are sent will be evaluated
    • If any one of the communications can proceed, he executes, and the others are ignored
    • If more than one case can be run, select randomly and fairly chooses one to execute, and the other does not execute
      Otherwise:
      1. If there is a default clause, the authority is executed;
      2. If there is no default clause, select blocks until a communication can run; Go does not re-evaluate the channel or value;

code example:

 PackageMainImport "FMT"funcMain () {varC1, C2, C3Chan int   varI1, I2int   Select{ CaseI1 = <-c1:fmt. Printf ("Received", I1,"from C1\ n") CaseC2 <-i2:fmt. Printf ("Sent", I2,"to C2\ n") Casei3, OK: = (&LT;-C3)://Same as:i3, OK: = <-c3         ifOK {fmt. Printf ("Received", i3,"from C3\ n")         }Else{FMT. Printf ("C3 is closed\ n")         }default: FMT. Printf ("No communication\ n")   }    }

The above code executes the result:

no communication

Go language "fifth": Go conditional statement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.