Flow control statement: 1. if statement, consisting of a Boolean expression followed by one or more statements; 2. "if...else" statement, the expression in else is false in the Boolean expression executed; 3. switch statement, used to perform different actions based on different conditions; 4. select statement; 5. for loop statement, syntax "for k,v := range oldmap{newmap[k]=v}"; 6. Loop control statements break, continue, goto.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Let’s take a look at the basic content of golang flow control statements.
is similar to the C language. The relevant conditional statements are as shown in the following table:
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 is executed when the Boolean expression is false. |
switch statement | The switch statement is used to perform different actions based on different conditions. |
select statement | The select statement is similar to the switch statement, but select will randomly execute a runnable case. If there is no case to run, it will block until there is a case to run. |
if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ }
if 布尔表达式 { /* 在布尔表达式为 true 时执行 */ } else { /* 在布尔表达式为 false 时执行 */ }
<span class="hljs-attribute">v</span>
can be of any type, val1
and val2
can be the same Any value of type, the type is not limited to constants or integers, or the final result is an expression of the same type. switch v { case val1: ... case val2: ... default: ... }
select { case communication clause : statement(s); case communication clause : statement(s); /* 你可以定义任意数量的 case */ default : /* 可选 */ statement(s); }
Note:
- Each case must be a communication
- All channel expressions will be evaluated and all sent The expressions will be evaluated
- If any one of the communications can be executed, it will be executed, and the others will be ignored
- If there are multiple cases that can be executed, select will randomly select one. implement.
- If no case can be run: If there is a default clause, the default clause will be executed, and the select will be blocked until a certain communication can run, thus avoiding the starvation problem.
Different from most languages, the loop statement in Go language only supports the for keyword and does not support the while and do-while structures. The basic usage of the keyword for is very close to that in C language and C .
For is used to implement loops in Go. There are three forms:
Syntax | |
---|---|
is the same as for in c language | for init; condition; post {} |
is the same as for in c language The while is the same as | for condition{} |
and <span class="hljs-function"><span class="hljs-title">for</span><span class="hljs-params">(;;) in c language </span></span> Same | for{} |
In addition, the for loop can also be used directlyrangeIterate over slices, maps, arrays and strings, etc., the format is as follows:
for key, value := range oldmap { newmap[key] = value }
Detailed explanation | |
---|---|
Interrupt out of the loop or switch statement | |
Skip the remaining statements of the current loop, and then continue with the next round of loops | |
will Control transfers to the marked statement |