Golang meridians flow control
I. Introduction Process Control
Process control is the variety of programming languages and to the control logic execution order of the traffic lights
of Go and if used for flow control, (the while not), as well as to simplify the code switch configuration, reducing code duplication born and goto statements, belonging to an expanded class process control.
Second, the basic flow control statements
Branch statements (if and switch)
Conditional statements (IF)
func main() {
var a int = 8
var b int = 10
var c int = 17
ifFunc(a)
ifFunc(b)
ifFunc(c)
}
//一般写法
func ifFunc(a int) {
if a > 10 {
fmt.Println(a, ">10")
} else if a == 10 {
fmt.Println(a, "=10")
} else {
fmt.Println(a, "<10")
}
}
OutPut Result:
8 <10
10 =10
17 >10
// 特殊写法
if err:=Connect();err != nil{
log.Fatal(err)
return
}
Special written, the return value is determined on the same line, and the scope of the return value is limited to if, else statement combination, so narrowing the scope of variables, since, in the programming, variables which achieves Variable Function after the role of sphere of influence, the smaller the possibility of problems caused.
Each variable represents a local state, stateful (scope), the state will be modified, local variables only affect the results of individual functions, but the entire program may affect global variables (all code) of the execution state, Therefore, limiting the role of the variable range of great help to the stability of the code.
Analyzing statements (switch): a simplified multiple conditional statement (if statement batch)
func switchFunc(v interface{}) {
// 使用断言判断类型
switch v.(type) {
case int, int8: // 一分支多值
fmt.Println("int")
case string:
fmt.Println("string")
default:
fmt.Println("Don't Know Type")
}
}
// OutPut:
int
string
Don't Know Type
fallthrough switch statement is executed across the
- By default, the switch statement is executed End corresponding code block will exit the entire switch, i.e., may not be required as C, C ++ and using the break statement as to end.
- If, after completion of the implementation of the current branch, want to continue the implementation of the following branches of code, you can use the keyword fallthrough achieved.
- fallthrough expression under a branch will not judge whether the statement is true, and enforced under a branch code.
func main() {
switch a := 1; {
case a == 1:
fmt.Println("1")
fallthrough
case a == 2:
fmt.Println("2")
// fallthrough
case a == 3:
fmt.Println("3")
case a == 4:
fmt.Println("4")
fallthrough
default:
fmt.Println("default")
}
}
Code block as shown above, the result outputs 12, since case 1, the use of fallthrough keywords, its determination does not meet the determination whether a branch will be enforced, the output 2, but does not perform case 3.
Loop (for), NO while
- For traversing the data structure such as an array of containers (array), the slice (slice), map (Map)
- Substantially circular configuration, similar to the while loop
- Infinite loop, very useful, such as persistent reading, running in the background
- The for loop can break, goto, return, panic forced to exit the loop statement.
func forFunc() {
var slice = make([]int, 10)
// 基本用法
for i := 0; i < 10; i++ {
slice[i] = i + 1
}
// 基本遍历切片
for i := 1; i <= 10; i++ {
fmt.Print(slice[i-1], "\t")
if i%4 == 0 {
fmt.Println()
}
}
fmt.Println()
// range 遍历切片
for i, v := range slice {
fmt.Print(i, ":", v, "\t")
if (i+1)%4 == 0 {
fmt.Println()
}
}
fmt.Println()
//无限循环
const N = 9
var i int
for {
if i > N {
break
}
// TODO
i++
}
}
// OutPut Result :
1 2 3 4
5 6 7 8
9 10
0:1 1:2 2:3 3:4
4:5 5:6 6:7 7:8
8:9 9:10
Example: for loop generated using the multiplication table:
func NineTable() {
for i := 1; i < 10; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d*%d=%2d \t", j, i, j*i)
}
// 手动换行
fmt.Println()
}
}
OutPut Result:
1*1= 1
1*2= 2 2*2= 4
1*3= 3 2*3= 6 3*3= 9
1*4= 4 2*4= 8 3*4=12 4*4=16
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
summary
- Go for language contains initialization statements, conditional expressions, the end of the statement, more than can be default.
- for range can iterate, slice, string, map, receiving channel
Jump statement (goto): Jump to the specified code label
- Unconditional jump quickly exit the multi-layer loop
- Unified error handling
Quick unconditionally withdraw from a multi-layer loop
// 跳出多层循环
func gotoLoop1() string {
for i := 0; i < 10; i++ {
for j := 0; j < 10; j++ {
if j == 5 {
fmt.Println(i, j)
// 跳转到 breakHere 标签
goto breakHere
}
}
}
return "return"
breakHere:
return "done"
}
Unified error handling
func errNomal() {
err := FirstcheckErr()
if err != nil {
fmt.Println(err)
exitProcess()
return
}
err = secondcheckErr()
if err != nil {
fmt.Println(err)
exitProcess()
return
}
fmt.Println("done")
}
func errGoto() {
err := FirstcheckErr()
if err != nil {
goto onExit
}
err = secondcheckErr()
if err != nil {
goto onExit
}
fmt.Println("done")
return
onExit:
fmt.Println(err)
exitProcess()
}
Third, loop control statements (break and continue)
break statement: Break out of the loop or switch judgment
- break statement can end for, switch and select a code block.
- Add tags after the break statement means exit a label corresponding code block.
func breakLoop() {
// 结束对应的循环
OUTLOOP:
for i := 1; i < 5; i++ {
for j := 0; j < 5; j++ {
switch j {
case 3:
fmt.Println(i, j)
break OUTLOOP
case 4:
fmt.Println(i, j)
break OUTLOOP
}
}
}
}
// Result:
//1 3
continue statement: for the end of the current cycle, for the start of the next cycle (for use only within the loop)
- continue statements only used within the for loop.
- After the continue statement label is added, it represents the next cycle into the cycle corresponding to the start tag.
func continueLoop() {
// 结束对应的循环
InerLOOP:
for i := 0; i < 2; i++ {
for j := 0; j < 5; j++ {
switch j {
case 2:
fmt.Println(i, j)
continue InerLOOP
}
}
}
}
// Result:
0 2
1 2