Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use conditional statements in Go language

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use conditional statements in Go language". In daily operation, I believe that many people have doubts about how to use conditional statements in GE language. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use conditional statements in Go language". Next, please follow the editor to study!

Conditional statements require developers to decide whether to execute the specified statement by specifying one or more conditions and testing whether the condition is true or not, and execute other statements if the condition is false.

The Go language provides the following conditional judgment statements:

Statement description if statement if statement consists of a Boolean expression followed by one or more statements. If...else statement the optional else statement can be used after the if statement. The expression in the else statement executes the if nested statement when the Boolean expression is false. You can embed one or more if or else if statements in the if or else if statement. Switch statements switch statements are used to perform different actions based on different conditions. The select statement select statement is similar to the switch statement, but select randomly executes a runnable case. If there is no case to run, it will block until case is available. If statement

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

Grammar

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

If Boolean expression {/ * executes * /} when Boolean expression is true

If executes the statement block immediately following when the Boolean expression is true, but not if it is false.

Example

Use if to determine the size of a number variable:

Package main import "fmt" func main () {/ * define the local variable * / var an int = 10 / * use the if statement to determine the Boolean expression * / if a < 20 {/ * execute the following statement * / fmt.Printf ("a < 20\ n")} fmt.Printf ("a value is:% d\ n", a)} if the condition is true

The result of the above code execution is:

The value of a less than 20a is: 10

If...else statement

The optional else statement can be used after the if statement, and the expression in the else statement executes when the Boolean expression is false.

Grammar

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

If Boolean expression {/ * execute * /} else {/ * when Boolean expression is true * /} when Boolean expression is false

If executes the statement block immediately following when the Boolean expression is true, or else statement block if false.

Example

Use if else to determine the size of a number:

Package main import "fmt" func main () {/ * local variable definition * / var an int = 100; / * determine Boolean expression * / if a < 20 {/ * execute the following statement * / fmt.Printf ("a less than 20\ n") if the condition is true } else {/ * if the condition is false, execute the following statement * / fmt.Printf ("an is not less than 20\ n");} fmt.Printf ("a value is:% d\ n", a);}

The result of the above code execution is:

The value of a not less than 20a is: 100

If statement nesting

You can embed one or more if or else if statements in if or else if statements.

Grammar

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

If Boolean expression 1 {/ * execute * / if Boolean expression 2 {/ * when Boolean expression 1 is true

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

Example

Use if statements in nesting:

Package main import "fmt" func main () {/ * Definitions local variable * / var an int = 100var b int = 100judgment condition * / if a = = 100{ / * if conditional statement executes for true * / if b = = 200{ / * if conditional statement executes * / fmt.Printf for true ("the value of an is 100,200 of b\ n") }} fmt.Printf ("a value:% d\ n", a); fmt.Printf ("b value:% d\ n", b);}

The result of the above code execution is:

The value of an is 100, the value of b is 200 a, the value of b is 200

Switch statement

Switch statements are used to perform different actions based on different conditions, and each case branch is unique, testing one by one from top to bottom until it matches.

The switch statement executes from top to bottom until a match is found, and no more break is required after the match.

Switch by default, case finally comes with a break statement. After a match, no other case will be executed. If we need to execute the following case, we can use fallthrough.

Grammar

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

Switch var1 {case val1:... Case val2:... Default:...}

The variable var1 can be of any type, while val1 and val2 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 end result is an expression of the same type.

You can test multiple values that may match the criteria at the same time, separating them with commas, such as case val1, val2, val3.

Flow chart:

Example

Package main import "fmt" func main () {/ * define the local variable * / var grade string = "B" var marks int = 90 switch marks {case 90: grade = "A" case 80: grade = "B" case 50 default: grade = "D"} switch {case grade = "A": fmt.Printf ("excellent!\" N ") case grade = =" B " Grade = "C": fmt.Printf ("good\ n") case grade = = "D": fmt.Printf ("pass\ n") case grade = = "F": fmt.Printf ("fail\ n") default: fmt.Printf ("poor\ n") } fmt.Printf ("your level is% s\ n", grade);}

The result of the above code execution is:

Excellent! Your grade is A

Type Switch

The switch statement can also be used in type-switch to determine the type of variable actually stored in an interface variable.

The format of the Type Switch syntax is as follows:

Switch x. (type) {case type: statement (s); case type: statement (s); / * you can define any number of case * / default: / * optional * / statement (s);}

Example

Package main import "fmt" func main () {var x interface {} switch I: = x. (type) {case nil: fmt.Printf (type of "x:% T" I) case int: fmt.Printf ("x is int") case float64: fmt.Printf ("x is float64") case func (int) float64: fmt.Printf ("x is func (int)") case bool String: fmt.Printf ("x is bool or string") default: fmt.Printf ("unknown")}}

The result of the above code execution is:

Type of x:

Fallthrough

Using fallthrough enforces the following case statement, and fallthrough does not determine whether the expression result of the next case is true.

Example

Package main import "fmt" func main () {switch {case false: fmt.Println ("1, case conditional statement is false") fallthrough case true: fmt.Println ("2, case conditional statement is true") fallthrough case false: fmt.Println ("3, Case conditional statement is false ") fallthrough case true: fmt.Println (" 4, case conditional statement is true ") case false: fmt.Println (" 5, case conditional statement is false ") fallthrough default: fmt.Println (" 6, default case ")}}

The result of the above code execution is:

2. Case conditional statements are true 3, case conditional statements are false 4, and case conditional statements are true.

From the output of the above code, you can see that switch starts execution from the first case that determines that the expression is true. If case has fallthrough, the program will continue to execute the next case, and it will not determine whether the expression of the next case is true.

Select statement

Select is a control structure in Go, similar to the switch statement used for communication. Each case must be a communication operation, either sending or receiving.

Select randomly executes a runnable case. If there is no case to run, it will block until case is available. A default clause should always run.

Grammar

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

Select {case communication clause: statement (s); case communication clause: statement (s); / * you can define any number of case * / default: / * optional * / statement (s);}

The syntax of the select statement is described below:

Each case must be a communication

All channel expressions are evaluated

All expressions sent will be evaluated

If any communication can be performed, it is executed and the others are ignored.

If more than one case can be run, the Select will randomly and fairly select one for execution. The rest will not be carried out.

Otherwise:

If there is a default clause, the statement is executed.

If there is no default clause, select blocks until a communication is running; Go does not re-evaluate the channel or value.

Example

Application demonstration of select statement:

Package main import "fmt" func main () {var C1, c2, c3 chan int var i1, i2 int select {case i1 =

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report