Process control of go language learning

Time:2020-9-7

01 if… Else… Standard use

In go, the conditional expression of an if… Else… Statement must be of boolean type. The parenthesis can be omitted, and the left brace cannot start on another line. Through the code, we demonstrate the standard use of if… Else.

func main() {
  x := 1
  if x >= 5 {
    fmt.Println("x >= 5")
  } else if x < 5 && x > 0 {
    fmt.Println("x < 5 && x > 0")
  } else {
    fmt.Println("x <= 0")
  }
}

Special use… Elf 02

If… Else… Statement is different from other languages. It supports initialization statement in conditional expression. It can define local variables (one or more) or execute initialization functions. The valid range of a local variable contains the entire if… Else… Block. Through the code, we demonstrate the use of if… Else… Initialization statements in conditional expressions.

func main() {
  if a, b := x+1, x+10; a < b {
    fmt.Println("a < b")
  } else {
    fmt.Println("a >= b")
  }
}

If… Else… Statements should pay attention to the matching order of conditions to avoid dead code, that is, code that will never be executed; try to reduce the nesting of code blocks, keep the single function of code blocks, and keep the normal logic at the same level. For complex composition conditions, it can be encapsulated into functions to improve the maintainability of the code.

03 use of switch standard

Switch statement is similar to if statement, which is also used to select execution. In go language, switch statement is different from switch statement in other languages. After matching case block, it is not necessary to execute break statement explicitly, and the program will not continue to execute the next case block automatically. If the program needs to continue to execute the next case block matching the case block, it can be executed at the end of the case block Fallthrough, but the conditional expression of the case block is no longer matched. If you want to prevent fallthrough, you can explicitly execute the break statement. When there are multiple matching conditions in a case block, hitting one of the conditions is enough, similar to the logical operator “or”. Through the code, we demonstrate the use of switch in go language.

func main() {
  a, b, c, x := 2, 3, 4, 2
  switch x {
  case a, b:
    println("x == a || x == b")
    fallthrough
  case c:
    println("x == c")
  case 3:
    println("x == 3")
  default:
    Println ("X does not meet any case conditions")
  }
}

04 special use of switch

The switch statement is similar to the if statement. It also supports the initialization statement. It matches the case execution from top to bottom and from left to some. The default block will be executed only when all case blocks fail to match. Through the code, we demonstrate the special use of switch statement.

func main() {
  switch y := 5; {
  case y > 5:
    fmt.Println("y > 5")
  case y > 0 && y <= 5:
    println("y > 0 && y <= 5")
  default:
    println("y = ", y)
  }
}

As for switch statements, there are two points to be noted in go language: one is that adjacent empty cases do not constitute multi condition matching; the other is that repeated case constant values cannot occur.

05 for standard use

In go language, there is only one loop statement for, but it can simulate while (condition) {} and while (true) {}. It should be noted that the initialization statement is executed only once. Through the code, we demonstrate how to use for loop in go language.

func main() {
  for i := 0; i < 5; i++ {
    println("i = ", i)
  }
}

06 for simulating while (condition) {}

func main() {
  j := 0
  for j < 5 {
    j++
    fmt.Println("j = ", j)
  }
}

07 for simulate while (true) {}

func main() {
  for {
    k++
    if k == 2 {
      continue
    }
    if k > 5 {
      break
    }
    fmt.Println("k = ", k)
  }
}

Traverse 08 for range

In go language, in addition to using the for standard form, traversal data can also use for… Range to traverse data. The supported data types include string, array, array pointer, slice, map, channel, and return index and key value data. So far, we have only introduced the string type in the above types, and we will introduce the other types later. Through the code, we demonstrate the use of for… Range traversal string to return Unicode.

func main() {
  str := "hello"
  for index := range str {
    fmt.Println(str[index])
  }
}

09 continue and break

In the for loop, continue and break can be used to jump out of the loop and end the loop respectively. Continue is only used for the for loop, while break can also be used in the switch statement introduced in 03 and the select statement we will introduce later. The use of continue and break is the same as the 07 for statement to simulate while (true) code demonstration.

10 goto

In go language, goto is used to realize fixed-point jump. Although there are different opinions about goto in the industry, in fact, some scenarios use goto to make the code more elegant. Before using goto, we need to define tags, which are case sensitive, and will cause compilation errors if the defined tags are not used. Note that goto cannot jump to other functions or inner code blocks. Through the code, we demonstrate the use of goto.

func main() {
  l := 0
  for l < 10 {
  start:
    l++
    if l < 5 {
      goto start
    }
    fmt.Println("l = ", l)
  }
}

Go Language Basics
Process control of go language learning