If else of process control

General writing

If expression 1 {branch 1} else if expression 2 {branch 2} else{branch 3}Copy the code

The instance

package main

import "fmt"

@author [email protected] @date 2020-09-29 00:00:39 @version 1.0 */
func main(a) {
   a, b, c := 1.2.3
   if a >b {
      fmt.Println(a)
   } else {
      fmt.Println(c)
   }
}
Copy the code

Special if else

A special way to write if conditions is by adding an execution statement before the if expression and then judging by the value of the variable

The instance

package main

import "fmt"

@author [email protected] @date 2020-09-29 00:00:39 @version 1.0 */
func main(a) {
   if a := 3; a > 0 {
      fmt.Println(a)
   }
}
Copy the code

Note: The Go language specifies that the open parenthesis {matching an if must be placed on the same line as the if and expression. Placing {elsewhere triggers a compilation error. Similarly, {matching else must be on the same line as else, and else must be on the same line as the curly brace to the right of the previous if or else if.

Switch of flow control

switch var1 {
    case val1:
        ...
    case val2:
        ...
    default:
        ...
}
Copy the code

The instance

package main

import "fmt"

@author [email protected] @date 2020-09-29 00:00:39 @version 1.0 */
func main(a) {

   switchTest(1)}func switchTest(a int)  {
   switch a {
   case 1:
      fmt.Println(1)
   case 2:
      fmt.Println(2)
   default:
      fmt.Println(a)

   }

}
Copy the code

Note:

  1. Switch statements are used to perform different actions based on different conditions. Each case branch is unique and matched from top to bottom until a match is made.
  2. The switch statement executes from top to bottom until a match is found without a break

type switch

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

switch x.(type){ case type: statement(s); case type: statement(s); /* You can define any number of case */ default: /* Optional */ statement(s); }Copy the code

The instance

package main

import "fmt"

/** type switch @author [email protected] @date 2020-09-29 00:00:39 @version 1.0 */
func main(a) {

   switchTest("")}func switchTest( a interface{})  {
   switch a.(type) {
   case int:
      fmt.Println(1)
   case float32:
      fmt.Println(2)
   default:
      fmt.Println("Unknown type")}}Copy the code

3. For loop of process control

Writing a

for init; condition; post { }
Copy the code

Description:

  • Init: usually an assignment expression that assigns an initial value to a control variable.
  • Condition: relational or logical expression, cyclic control condition;
  • Post: Usually an assignment expression that increments or decays a control variable.

The instance

package main

import "fmt"

@author [email protected] @date 2020-09-30 11:30:57 @version 1.0 */
func main(a) {
   forDemo(10)}func forDemo(y int) {
   for x := 0; x < y; x++ {
      fmt.Println("Hello World", x)
   }
}
Copy the code

The output

Hello World 0
Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5
Hello World 6
Hello World 7
Hello World 8
Hello World 9
Copy the code

Write two

for condition { }
Copy the code

The instance

package main

import (
   "fmt"
)

@author [email protected] @date 2020-09-30 11:30:57 @version 1.0 */
func main(a) {
   forDemo1()

}

func forDemo1(a) {
   y := 0
   for ; y < 10; y++ {
      fmt.Println("Ferrayman",y)
   }
}
Copy the code

The output

Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 3
Ferrayman 4
Ferrayman 5
Ferrayman 6
Ferrayman 7
Ferrayman 8
Ferrayman 9
Copy the code

Write three

The instance

package main

import (
   "fmt"
)

@author [email protected] @date 2020-09-30 11:30:57 @version 1.0 */
func main(a) {

   forDemo2()
}

func forDemo2(a) {
   x := 10
   for x > 0 {
      fmt.Println("Ferrayman", x)
      x--
   }
}
Copy the code

The output

Ferrayman 10
Ferrayman 9
Ferrayman 8
Ferrayman 7
Ferrayman 6
Ferrayman 5
Ferrayman 4
Ferrayman 3
Ferrayman 2
Ferrayman 1
Copy the code

4 process control for range

Go allows for range to iterate over groups, slices, strings, maps, and channels. The return value traversed through for range has the following pattern:

  1. Arrays, slicing, and strings return indexes and values.
  2. Map returns keys and values.
  3. A channel returns only the value within the channel.

I won’t go into details here, but I’ll go into details later when WE learn about data structures.

5 break Breaks the loop

The break statement terminates blocks of code for, switch, and SELECT.

The instance

package main

import (
	"fmt"
)

@author [email protected] @date 2020-09-30 22:45:55 @version 1.0 */
func main(a) {

		for j := 5; j > 0; j-- {
			for i := 0; i < 5; i++ {
				if i == 3 {
					break
				}
				fmt.Println("Ferrayman",i)

			}
		}

}

Copy the code

The output

Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 0
Ferrayman 1
Ferrayman 2
Copy the code

The break statement can also be followed by a label to exit the code block corresponding to a label. The label must be defined on the corresponding for, switch, and SELECT code blocks.

package main

import (
   "fmt"
)

@author [email protected] @date 2020-09-30 22:45:55 @version 1.0 */
func main(a) {
   Tag:
      for j := 10; j > 0; j-- {
         for i := 0; i < 10; i++ {
            if i == 5 {
               break Tag
            }
            fmt.Println("Ferrayman",i)

         }
      }

}
Copy the code

The output

Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 3
Ferrayman 4
Copy the code

6 continue Skips this loop and continues with the next loop

The instance

package main

import "fmt"

@author [email protected] @date 2020-09-30 22:45:55 @version 1.0 */
func main(a) {
   for i := 0; i < 10; i++ {
      if i == 5 {
         continue
      }
      fmt.Println("Ferrayman",i)

   }
}
Copy the code

The output

Ferrayman 0
Ferrayman 1
Ferrayman 2
Ferrayman 3
Ferrayman 4
Ferrayman 6
Ferrayman 7
Ferrayman 8
Ferrayman 9
Copy the code

Note: Continue is only used in for loops

6 Keyword goto

The GOTO statement of the Go language can be unconditionally moved to the line specified in the procedure.

The goto keyword is not recommended. It makes the program hard to understand.

The instance

Package main import (" FMT ") @author [email protected] @date 2020-09-30 22:45:55 @version 1.0 */ Func main() {Tag1: FMT.Println("haha, I come again ") for I := 0; i < 5; Println("Ferrayman", I)} Tag: Println("hehe, I, I ") goto Tag1}Copy the code

The output

Ferrayman 0 Ferrayman 1 Ferrayman 2 Endless loop... And the first print is on line 23 of the programCopy the code

(Tag and tag1 are not executed in the same order each time. I think this is related to the parallel programming design of GO language.)