1. Process control of Golang development

Golang development process control

1. Conditional judgment (if)

The Go language if condition judgment format is as follows:

if expression 1 { 
branch 1
} else if expression 2 {
branch 2
} else {
branch 3
}

Go stipulates that the left parenthesis "{" that matches if must be placed on the same line as if and expression. If you try to place "{" in another position, a compilation error will be triggered, as is the case of else.

Examples:

var ten int = 11

if ten > 10 {
fmt.Println(">10")
} else {
fmt.Println("<=10")
}

Special wording:

You can add an execution statement before the if expression, and then judge based on the value of the variable:

if err := Connect(); err != nil {
fmt.Println(err)
return
}

err! = nil is the judgment expression of if. When err is not empty, an error is printed and returned.

 

2. Build a loop (for)

The for loop format is as follows:

for initial statement ; conditional expression ; end statement { 
loop body code
}

2.1. The initial statement in the for statement executed at the beginning of the loop

The initial statement is the statement executed before the first loop, and its scope will be limited to the scope of this for.
The initial statement can be ignored, but the semicolon after the initial statement must be written:

step := 2
for ; step > 0; step-- {
fmt.Println(step)
}

There is no initial statement in this code for, and the scope of step at this time is larger than the step statement in the initial statement.

2.2. Conditional expressions in for-a switch that controls whether to loop

2.2.1. Endless loop with executable statement at the end of the loop
var i int
for ; ; i++ {
if i > 10 {
break
}
}

2.2.2. Infinite loop

var i int
for {
if i > 10 {
break
}
i++
}
2.2.3. Cycle with only one cycle condition
var i int

for i <= 10{
i++
}

 

Guess you like

Origin www.cnblogs.com/Wshile/p/12681185.html