Go Control Now we've learned about data, types, and functions. Next we will discuss another important issue: the control statements provided by the go language. In fact, the go language provides only a few control structures, which greatly simplifies the use of control statements. The go language is a block-structured programming language that uses "{}" to block a set of code. If you have been in strange other programming languages often used in the ";" Where I go, I can tell you clearly that it still exists in go, but it will automatically add ";" To you during the compilation process. If you also add semicolons to the end of the code, the compiler will consider them to be unwanted characters and automatically reject the semicolons. The For loop is the only loop in the go language. The For loop can be used to create conditional loops and enumerate loops. The For loop has the following form: For condition { Operation } Note that you do not need to place the condition of the loop in a pair of curly braces "{}". The loop terminates when the condition is not met. The loop will check whether the condition is satisfied before each execution of the loop body, so the loop body can be executed 0 or more times, similar to the while loop. Cases: I:=0 For a<10 { Fmt.print (a) A=a+1 } You can create a loop that does not terminate by using for true {"or" for {"). The enumeration loops are basically the same as other C-like languages: For -expression 1 ; conditions ; Expression 3 { Operation } Expression 1 executes once before the loop starts, and expression 3 executes once each time the loop body executes, and the condition statement is checked before each loop body execution, and if true resumes the loop. Cases: For i:=0; i<10; i++ { Fmt.print (a) } You can include any statement in the for expression, but only if you have to add a semicolon to distinguish which part of the expression your statement belongs to. There is one exception, however, where you create a conditional expression without a conditional statement. You can also repeatedly declare values in an array, slice, string, map, or channel in the for expression, using a For loop similar to that in other languages. For example: var array= [] int {1,2,3,4} For i,v:= range Array { Fmt.print (I,V) } The number of loops for the for expression depends on the size of the index and the array, like here I and V. |