[Go process control structure]

Posted by joad on Wed, 08 Dec 2021 06:30:57 +0100

Process control statements are used to control the execution order of the program, so that the program has a logical structure. General process control statements need to be used in combination with various conditions, such as conditional judgment if, switch for selection, and for loop.

if conditional statement

If statement is a conditional statement. It determines which branch to execute according to the Boolean expression: if the value of the expression is true, the if branch is executed; If the value of the expression is false, the else branch is executed.

There are some rules for using if conditional statements:

  1. The conditional expression after if does not need to use (), which is different from some programming languages, which more reflects the simplicity of Go language;

  2. Curly braces in each conditional branch (if or else) are required, even if there is only one line of code in the curly braces;

  3. if the curly braces immediately following (can't monopolize a line, and the curly braces before else) can't monopolize a line, otherwise the compilation won't pass;

  4. Multiple else conditional branches can be added to the if... Else conditional statement. However, this method is not recommended because the code readability is poor. Multiple conditional branches can be replaced by switch to make the code more concise.

 func main() {
     if i := 6; i > 10 {
         fmt.Println("I > 10")
     } else if i > 5 && i <= 10 {
         fmt.Println("5<i<=10")
     } else {
         fmt.Println("i <= 5")
     }
 }

Different from other programming languages, the if statement of Go language can have a simple expression statement, and the statement and conditional statement use semicolons; separate. Variables declared by simple if statements can only be used in the whole if... else if... else conditional statements. As shown in the example above.

switch selection statement

If conditional statements are more suitable for the case of fewer branches. If there are many branches, it will be more convenient to select switch. The switch statement of Go language is very powerful and flexible. The switch statement can also be initialized with a simple statement, also with a semicolon; division. Each case is a branch. The branch will be executed only if the branch condition is true, and the conditional expression after the case score cannot be wrapped in parentheses ().

In Go language, the case of switch is judged one by one from top to bottom. Once the conditions are met, the corresponding branch is executed and returned immediately, and the other branches are no longer judged. That is, by default, the switch of Go language has a break at the end of the case. This is different from other programming languages. The Go language is designed to prevent writing a break and the next case is executed. What if you really need to execute the next following case? The Go language provides the fallthrough keyword, which can only be used as the last non empty statement in the sub sentence.

  • fallthrough cannot be used on the last branch of switch

  • When fallthrough to the next case, the case matching check is not performed

 switch {
     case false:
         fmt.Println("The integer was <= 4")
         fallthrough
     case true:
         fmt.Println("The integer was <= 5")
         fallthrough
     case false:
         fmt.Println("The integer was <= 6")
         fallthrough
     case true:
         fmt.Println("The integer was <= 7")
         fallthrough
     case false:
         fmt.Println("The integer was <= 8")
     default:
         fmt.Println("default case")
 }
 Output results:
 The integer was <= 5
 The integer was <= 6
 The integer was <= 7
 The integer was <= 8

In this example, there are not only simple initialization statements, but also expressions. When there is an expression after switch, the value after case must be the same as the result type of this expression. For example, if i here is of type int, then the value after case can only be of type int. If it is another type, it will prompt that the type does not match and cannot be compiled.

The expression after switch does not have too many restrictions. It is only a legal expression, and it is not necessarily required to be a constant or integer. You can directly put the comparison expression after switch:

 switch 2 > 1 {
 case true :
     fmt.Println("true")
 case false :
     fmt.Println("false")
 }

for loop statement

The classic for loop consists of three parts, two of which are required; division.

 sum := 0
 for i := 1; i <= 100; i++ {
     sum += i
 }
 fmt.Println("the sum is ", sum)
  • The first part is a simple statement, which is generally used for the initialization of the for loop, such as declaring and initializing the variable i.

  • The second part is the condition of the for loop, which indicates when the for loop ends.

  • The third part is the update statement, which is generally used to update loop variables.

It should be noted that the for loop of Go language is very powerful. The above three parts are not necessary and can be omitted. Like other programming languages, there are while loop statements. There is no while loop in Go language, but the while effect can be achieved through the for loop. In Go language, continue and break are also supported to control for loops:

  1. Continue can jump out of this cycle and continue to execute the next cycle.

  2. Break can jump out of the whole for loop. Even if the for loop is not completed, it will be forcibly terminated.

 i, sum := 1, 0
 for {
     sum += i
     i++
     if i>100{
         break
     }
 }
 fmt.Prinltn("the sum is", sum)

The above example uses a for loop without any conditions, also known as a for infinite loop. Use break to exit the infinite loop.

Topics: Go Back-end