5. Process control of Go language

Process control includes three major categories: conditional judgment, loop control and unconditional jump.

if

if is perhaps the most common among various programming languages. Its syntax can be summarized as: if the condition is met, do something, otherwise do another thing.

There is no need for parentheses in the if conditional judgment statement in Go, as shown in the following code

if x > 10 {
    
    
	fmt.Println("x is greater than 10")
} else {
    
    
	fmt.Println("x is less than 10")
}

Another powerful thing about Go's if is that it allows you to declare a variable in a conditional judgment statement . The scope of this variable can only be within the conditional logic block, and it will not work elsewhere, as shown below

//  计算获取值 x, 然后根据 x 返回的大小,判断是否大于 10 。
if x := computedValue(); x > 10 {
    
    
	fmt.Println("x is greater than 10")
} else {
    
    
	fmt.Println("x is less than 10")
}
// 这个地方如果这样调用就编译出错了,因为 x 是条件里面的变量
fmt.Println(x)

When there are multiple conditions, it is as follows:

if integer == 3 {
    
    
	fmt.Println("The integer is equal to 3")
} else if integer < 3 {
    
    
	fmt.Println("The integer is less than 3")
} else {
    
    
	fmt.Println("The integer is greater than 3")
}

goto

Go has the goto statement - use it wisely. Use goto to jump to a label that must be defined within the current function. For example, consider a loop like this:

func myFunc() {
    
    
	i := 0
Here: // 这行的第一个词,以冒号结束作为标签
    println(i)
    i++
    goto Here // 跳转到 Here 去
}

Tag names are case-sensitive.

for

The most powerful control logic in Go is for, which can be used to read data in a loop, and can be used as a while to control logic, and it can also operate iteratively. Its syntax is as follows:

for expression1; expression2; expression3 {
    
    
	//...
}

expression1, expression2 and expression3 are all expressions, where expression1 and expression3 are variable declarations or
function call return values, expression2 is used for conditional judgment, expression1 is called before the start of the loop, and expression3 is
called at the end of each cycle.
An example is more useful than the above, so let's take a look at the following example:

package main
import "fmt"
func main(){
    
    
	sum := 0;
    for index:=0; index < 10 ; index++ {
    
    
	    sum += index
    }
    fmt.Println("sum is equal to ", sum)
}
//  输出: sum is equal to 45

Sometimes multiple assignment operations are needed. Since there is no, operation in Go, you can use parallel assignment i, j = i+1, j-1.
Sometimes if we ignore expression1 and expression3:

sum := 1
for ; sum < 1000; {
    
    
	sum += sum
}

Among them; can also be omitted, then it will become the following code. Does it sound familiar? Yes, this is what while does.

sum := 1
for sum < 1000 {
    
    
	sum += sum
}

There are two key operations in the loop: break and continue. The break operation is to jump out of the current loop, and the continue operation is to skip this loop. When the nesting
is too deep, break can be used with labels, that is, jumping to the location specified by the label. For details, refer to the following example:

for index := 10; index>0; index-- {
    
    
    if index == 5{
    
    
        break //  或者 continue
    }
	fmt.Println(index)
}
// break 打印出来 10 、 9 、 8 、 7 、 6
// continue 打印出来 10 、 9 、 8 、 7 、 6 、 4 、 3 、 2 、 1

break and continue can also be followed by labels to jump to the outer loop in a multiple loop.

for combined with range can be used to read slice and map data :

for k,v:=range map {
    
    
	fmt.Println("map's key:",k)
	fmt.Println("map's val:",v)
}

Since Go supports "multiple value returns", the compiler will report an error for variables that are "declared but not called". In this case, you can use _ to discard
the unnecessary return values . For example

for _, v := range map{
    
    
	fmt.Println("map's val:", v)
}

switch

Sometimes you need to write a lot of if-else to implement some logical processing. At this time, the code looks ugly and lengthy, and it is not easy to maintain in the future. At this time, switch can solve this problem very well. Its syntax is as follows

switch sExpr {
    
    
    case expr1:
    	some instructions
    case expr2:
    	some other instructions
    case expr3:
    	some other instructions
    default:
    	other code
}

The types of sExpr and expr1, expr2, expr3 must be consistent. Go's switch is very flexible. The expression does not have to be a constant or integer. The execution process is from top to bottom until a match is found; and if the switch has no expression, it will match true.

i := 10
switch i {
    
    
    case 1:
    	fmt.Println("i is equal to 1")
    case 2, 3, 4:
    	fmt.Println("i is equal to 2, 3 or 4")
    case 10:
    	fmt.Println("i is equal to 10")
    default:
    	fmt.Println("All I know is that i is an integer")
}

In line 5, we aggregate many values ​​into one case. At the same time, the switch in Go is equivalent to a break at the end of each case . After a successful match, other cases will not be automatically executed downwards, but the entire switch will be jumped out. , but you can use fallthrough to force execution of the subsequent case code .

integer := 6
switch integer {
    
    
    case 4:
    	fmt.Println("The integer was <= 4")
    	fallthrough
    case 5:
    	fmt.Println("The integer was <= 5")
    	fallthrough
    case 6:
    	fmt.Println("The integer was <= 6")
    	fallthrough
    case 7:
        fmt.Println("The integer was <= 7")
        fallthrough
    case 8:
        fmt.Println("The integer was <= 8")
        fallthrough
    default:
    	fmt.Println("default case")
}
// 上面的程序将输出
The integer was <= 6
The integer was <= 7
The integer was <= 8
default case

Guess you like

Origin blog.csdn.net/u012534326/article/details/120028905