This is the 7th day of my participation in Gwen Challenge

These reviews

The previous article mainly introduced the basic concepts and usage of Pointers in Go language and the basic use of constants and aliases. The implementation of code generally has three structures:

  • Sequential structure: Code is executed from top to bottom
  • Branch structure: Execute different statements according to different conditions
  • Loop structure: Executes a piece of code repeatedly based on specified conditions.

Sequential structure is our most common implementation, relatively simple, in this article we will learn the Go language branch and loop structure usage.

Branch and loop control

Golang’s branching controls are consistent with those of other languages, and the simple expression is as follows:

if expression1 {
	branch1
} else if expression2 {
	branch2
} else {
	branch3
}
Copy the code

Golang specifies that {matching if must be on the same line as if and expression, or a compilation error will occur. Similarly, else must be on the same line as the} of the previous branch. You can omit () from both sides of the expression.

In addition to the if key, Golang also provides switch statements to determine a large number of values and expressions. In order to avoid human error, each case in the switch is an independent code block. There is no need to break out of the switch selection body through the break keyword. If you need to continue to perform the following case judgment, You need to add the fallthrough keyword to connect the upper and lower cases. In addition to supporting numeric constants, Golang’s Switch can handle complex situations like strings and expressions. A simple example is as follows:

// Assign jobs according to names
name := "Little red"
switch name {
case "Xiao Ming":
	fmt.Println("Sweeping")
case "Little red":
	fmt.Println("Clean the blackboard")
case "Xiao gang":
	fmt.Println("Take out the garbage")
default:
	fmt.Println("No one is working.")}Copy the code

In the above code, each case is a string style and does not require a break control to exit.

If we need to judge the expression in a case, in this case switch is no longer followed by the judge variable, and there is no target to judge, as shown in the following example:

// Determine the degree of achievement according to the score
score := 90
switch  {
case score < 100 && score >= 90:
	fmt.Println("Good")
case score < 90 && score >= 80:
	fmt.Println("Good")
case score < 80 && score >= 60:
	fmt.Println("Pass")
case score < 60 :
	fmt.Println("Fail")
default:
	fmt.Println("Wrong score")}Copy the code

The body of the Golang loop only provides the for keyword and does not have the while or do-while form provided in other languages. The basic style is as follows:

forinit; condition; End {loop body code}Copy the code

The initial statement, conditional expression, and closing statement are all defaultable. If all three default, this becomes an infinite loop statement. You can break out of the loop using the break keyword, and continue to the next loop using the continue keyword.

summary

This article mainly introduces the branch and loop control structure of Go based syntax. The keywords, such as If, Switch and for, are commonly used in our daily coding. When we operate a large number of variables of the same type in the program, we need to use the power of containers to facilitate data storage and manipulation. Let’s look at containers in Go.