This is a creation in Article, where the information may have evolved or changed.
Process Control
The main record of this section is the process control in the programming language that handles the logical structure.
Basically, the Process Control statements start 3 functions:
Select to jump to a different execution sequence according to the criteria
Loop, repeatedly executing a sequence
Jump, return to an execution sequence according to condition
The go language supports several flow control statements such as the following:
conditional statements, the corresponding keywords are if, else and else if;
Select the statement, the corresponding keyword is switch, case and select (will be described in the introduction of the channel);
loop statements, corresponding keywords for and range;
Jump statement, the corresponding keyword is goto.
The go language also added keywords: break, continue, Fallthrough. We have to use these keywords flexibly in actual development to increase the robustness of the code.
If...else If...else
If a < 5 {return 0} else {return 1}
For conditional statements, the following points need to be noted:
Conditional statements do not need parentheses to enclose conditions ();
The curly braces {} must exist regardless of the statement body.
The left curly brace {must be in the same row as if or else;
After the IF, the conditional statement can be added before the variable initialization statement, the use of; Interval
In a function that has a return value, the "final" return statement is not allowed to be included in the If...else ... Structure, otherwise the compilation fails:
The go compiler cannot find the return statement that terminates the function. The following are examples of failed compilations:
func example (x int) int {if x = = 0 {return 5} else {return x}}
Switch
Switch I {case 0:fmt. Printf ("0") case 1:fmt. Printf ("1") case 2:fallthrough case 3:fmt. Printf ("3") Case 4, 5, 6:fmt. Printf ("4, 5, 6") default:fmt. Printf ("Default")}
Running the above case, you will get the following results:
i = 0 o'clock, output 0;
i = 1 o'clock, output 1;
i = 2 o'clock, output 3;
i = 3 o'clock, output 3;
i = 4 o'clock, output 4, 5, 6;
i = 5 o'clock, output 4, 5, 6;
i = 6 o'clock, output 4, 5, 6;
i = Any other value, the output is default.
In contrast to other languages, the expression after switch is not even required, such as the following example:
Switch {case 0 <= num && num <= 3:fmt. Printf ("0-3") Case 4 <= num && num <= 6:fmt. Printf ("4-6") Case 7 <= num && num <= 9:fmt. Printf ("7-9")}
Switch default selection evaluates true to execute the first case is a true expression
When using the switch structure, we need to pay attention to the following points:
Left Curly brace {must be in the same row as switch;
A conditional expression is not limited to a constant or an integer;
Multiple result options can appear in a single case;
Contrary to the rules of C, the go language does not need to use a break to explicitly exit a case;
The next case will be executed only if the Fallthrough keyword is explicitly added to the case;
You can not set the conditional expression after switch, in which case the entire switch structure with multiple
If...else ... The logical function of the same.
for the loop statement in the go language only supports the FOR keyword and does not support the while and do-while structures
Sum: = 0for I: = 0; I < 10; i++ {sum + = i}
The for-keyword expression is not included
The infinite loop of the go language is also very simple, such as:
Sum: = 0for {sum++ If sum > {break}}
Multi-assignments are also supported in conditional expressions, such as:
A: = [] Int{1, 2, 3, 4, 5, 6}for i, J: = 0, Len (a) –1; I < J; I, j = i + 1, j–1 {a[i], a[j] = A[j], A[i]}
There are a few things to keep in mind when using loop statements.
The left curly brace {must be in the same row as for.
The For loop in the go language, like the C language, allows you to define and initialize variables in the loop condition, the only difference
Yes, the go language does not support multiple assignment statements separated by commas and must be initialized with a parallel assignment (: =) to initialize multiple
A variable.
The For loop of the Go language also supports continue and break to control loops, but it provides a more advanced
Break, you can choose which loop to interrupt, such as:
For j: = 0; J < 5; J + + {for I: = 0, I <; i++ {if i > 5 {break Jloop} fmt. Println (i)}}jloop://...
In this example, the break statement terminates the outer Loop at the Jloop label
Goto
The go language supports the GOTO keyword, and the semantics of a goto statement is very simple, which is to jump to a tag within this function, such as:
Func MyFunc () {i: = 0 here:fmt. Println (i) i++ if I < {goto here}}