This is the 17th day of my participation in the August Text Challenge.More challenges in August

The content of this article comes from my study of “Go language minimalist a General” and “Go Web programming practical school” and their own practice summary

In the process of programming, often according to a series of conditions to make different operations; This process is called flow control

if

var list []string = []string{"future"."Go"}
if len(list) > 2 {
  fmt.Println("List is longer than 2")}Copy the code

At the heart of every if statement is an expression with a value of true or false, called a conditional test

  • Check for equality

    num1 := 1
    num2 := 2
    if num1 == num2 {
      fmt.Println("They're equal.")}Copy the code
  • Numeric comparison

    Int, int8, int16, int32, int64

  • Multicondition check

    • Or (| |)
    • And (&)
    • Not (!)
  • Boolean expression

    canEat := false
    canGo := true
    Copy the code
  • If – else structure

    When more than two conditions need to be checked, if-else deconstruction provided by Go can be used. In a programming language, simply execute a block of code in if-else deconstruction, which checks each conditional test in turn until it passes; After passing the test, the logical code immediately following is executed and the other tests are skipped

    var list []string = []string{"future"."Go"}
    if len(list) > 2 {
      fmt.Println("List is longer than 2")}else {
      fmt.Println("The length of the list is not greater than 2")}Copy the code

switch

When there is a condition, some if-else may be difficult to read and maintain later. In Go, switch can also control the flow

var nums = []int{1.2.3.4.5.6.7}
for _, num := range nums {
  switch num {
    case 1:
    fmt.Println("Monday")
    case 2:
    fmt.Println("Tuesday")
    case 3:
    fmt.Println("Wednesday")
    case 4:
    fmt.Println("Thursday")
    case 5:
    fmt.Println("Friday")
    case 6:
    fmt.Println("Saturday")
    case 7:
    fmt.Println("Sunday")}}Copy the code
  • The left brace{Must be connected toswitchThe keywords are on the same line
  • You don’t need it in GobreakTo derive one explicitlycaseclause
  • Only in thecaseClause to explicitly addfallthroughKeyword will continue to execute immediately after the next onecaseclause

Looping statements

The switch example above uses one type of loop. In Go, there are two types of loop: for and for range

Here are two ways to make a small demo:

// iterate from one to ten
for i := 0; i < 10; i++ {
  fmt.Println(i)
} 

Copy the code

The print result is as follows:


// Iterate over the elements in front and print
front := [...]string{"html"."css"."javascript"."typescript"."vue"."react"."Wechat Mini Program"}
for _, v := range front {
  fmt.Println(v)
}
Copy the code

The traversal results are as follows:

break

The effect of break is to execute the middle section backward, out of the current scope

var (
  sum int
  i   = 1
)

for {
  if i > 5 {
    break
  }

  sum += i

  fmt.Println(i, "-- >", sum)

  i++
}

fmt.Println(sum)
Copy the code

When you use for, the loop keeps going if it is not followed by a condition.

In the example above, the initial value I is 1. Each time I is executed in the body of the loop, I is incremented by one and the current I and summation are printed. If I is greater than 5 it jumps out of the current loop body

continue

var (
  sum int
  i   = 1
)

for {
  if i > 10 {
    break
  }

  if i%2! =0 {
    // this continue creates an endless loop
    // since the code never increases the i
    continue
  }

  sum += i

  fmt.Println(i, "-- >", sum)

  i++
}

fmt.Println(sum)
Copy the code

If you’re a beginner and you get this code to run, you’re probably confused. Why not print it out? Actually!!!! Continue is playing tricks; Sum (int); sum (int); sum (int); sum (int); After entering the loop, check if I is greater than 10. If I is greater than 10, exit the current loop. If I is less than 10, continue to execute. Sum += I = sum + I = sum + I = sum + I = sum + I = sum + I = sum + I = sum + I Then a second judgment is made to determine whether modulo 2 of I is equal to 0. If it is not equal to 0, continue. The first line of the for loop is executed to determine whether it is greater than 10 and whether modulo 2 is equal to 0. Because I is always 1, the first judgment is never valid, and the second judgment is always executed, over and over again; How to break the logjam in the code above? The idea is to change the initial value of “I” before “continue” so that we don’t get stuck in an infinite loop.

var (
  sum int
  i   = 1
)

for {
  if i > 10 {
    break
  }

  if i%2! =0 {
    // this continue creates an endless loop
    // since the code never increases the i
    i++ Change the value of I before continue
    continue
  }

  sum += i

  fmt.Println(i, "-- >", sum)

  i++
}

fmt.Println(sum)
Copy the code

The print result is as follows:

Write a multiplication table of 99:

// Decide which row to process
for i := 1; i < 10; i++ {
  // Determine how many columns there are in this row
  for j := 1; j <= i; j++ {
    fmt.Printf("%d x %d = %d \t", j, i, i*j)
  }
  // Manually generate carriage returns
  fmt.Println("")}// Decide which row to process
for row := 1; row < 10; row++ {
  // Decide which column to process
  for col := row; col < 10; col++ {
    fmt.Printf("%d x %d = %d \t", row, col, row * col)
  }
  fmt.Println("")}Copy the code

The results are as follows:

goto

In the Go language, you can jump to a tag through the GOTO statement for unconditional jump between codes. The GOto statement also helps in breaking out of the loop quickly and avoiding repeated exits. Using goto statements can simplify some code implementation.

func learnGoto(a) {
	var i int
loop:
	if i < 3 {
		fmt.Println("looping")
		i++
		goto loop
	}
	fmt.Println("done")}Copy the code