Process control is an important part of each programming language to control logic and execution order
Go language most commonly used with a flow control if
and for
, and switch
, and goto
primarily to simplify the code, the code decrease repetition born structure, belonging to class flow control extension.
if else (branched structure)
if condition is determined substantially wording
Go language if
conditional format is as follows:
1 IF Expression 1 { 2 branch 1 . 3 } the else IF expression 2 { 4 branch 2 . 5 } the else { . 6 branched. 3 . 7 }
When the result of the expression is 1 true
when performing a branch, or determination expression 2, if the branch is 2 is satisfied, not satisfied, a branch 3 is performed. if the judgment else if
and else
are optional, can be selected according to actual needs.
Go language provisions of the if
left bracket match {
must be if和表达式
on the same line on the, {
on the other position will trigger a compilation error. Similarly, the else
match {
must be with else
the same line, else
also must be on one if
or else if
braces on the right on the same line.
eg:
1 func ifDemo1() { 2 score := 65 3 if score >= 90 { 4 fmt.Println("A") 5 } else if score > 75 { 6 fmt.Println("B") 7 } else { 8 fmt.Println("C") 9 } 10 }
determining if a special condition wording
determining if there is a special condition wording may be added prior to a statement executed if expression, then determining, in accordance with the variable value EG:
1 func ifDemo2() { 2 if score := 65; score >= 90 { 3 fmt.Println("A") 4 } else if score > 75 { 5 fmt.Println("B") 6 } else { 7 fmt.Println("C") 8 } 9 }
for (loop structure)
All types of loops all Go language can use for
keywords to complete.
The basic format for the following cycle:
. 1 for initial statement; conditional expression; end statement { 2 loop statement 3 }
Conditional expression returns the true
body of the loop have been conducting cycles until the condition expression returns false
automatically exit the cycle time.
1 func forDemo() { 2 for i := 0; i < 10; i++ { 3 fmt.Println(i) 4 } 5 }
for loop initial statement can be ignored, but the semicolon after the initial statement must be written, for example:
1 func forDemo2() { 2 i := 0 3 for ; i < 10; i++ { 4 fmt.Println(i) 5 } 6 }
for loop statement and the initial end statement can be omitted, for example:
1 func forDemo3() { 2 i := 0 3 for i < 10 { 4 fmt.Println(i) 5 i++ 6 } 7 }
The wording is similar to other programming languages while
, in while
a continuous loop After adding a conditional expression, satisfying the conditional expression, or the end of the cycle.
Infinite loop
1 for { 2 循环体语句 3 }
for循环可以通过break
、goto
、return
、panic
语句强制退出循环。
Go语言中可以使用for range
遍历数组、切片、字符串、map 及通道(channel)。 通过for range
遍历的返回值有以下规律:
- 数组、切片、字符串返回索引和值。
- map返回键和值。
- 通道(channel)只返回通道内的值。
switch case
使用switch
语句可方便地对大量的值进行条件判断。
1 func switchDemo1() { 2 finger := 3 3 switch finger { 4 case 1: 5 fmt.Println("大拇指") 6 case 2: 7 fmt.Println("食指") 8 case 3: 9 fmt.Println("中指") 10 case 4: 11 fmt.Println("无名指") 12 case 5: 13 fmt.Println("小拇指") 14 default: 15 fmt.Println("无效的输入!") 16 } 17 }
Go语言规定每个switch
只能有一个default
分支。
一个分支可以有多个值,多个case值中间使用英文逗号分隔。
1 func testSwitch3() { 2 switch n := 7; n { 3 case 1, 3, 5, 7, 9: 4 fmt.Println("奇数") 5 case 2, 4, 6, 8: 6 fmt.Println("偶数") 7 default: 8 fmt.Println(n) 9 } 10 }
分支还可以使用表达式,这时候switch语句后面不需要再跟判断变量。例如:
1 func switchDemo4() { 2 age := 30 3 switch { 4 case age < 25: 5 fmt.Println("好好学习吧") 6 case age > 25 && age < 35: 7 fmt.Println("好好工作吧") 8 case age > 60: 9 fmt.Println("好好享受吧") 10 default: 11 fmt.Println("活着真好") 12 } 13 }
fallthrough
语法可以执行满足条件的case的下一个case,是为了兼容C语言中的case设计的。
1 func switchDemo5() { 2 s := "a" 3 switch { 4 case s == "a": 5 fmt.Println("a") 6 fallthrough 7 case s == "b": 8 fmt.Println("b") 9 case s == "c": 10 fmt.Println("c") 11 default: 12 fmt.Println("...") 13 } 14 }
输出:
a b
goto(跳转到指定标签)
goto
语句通过标签进行代码间的无条件跳转。goto
语句可以在快速跳出循环、避免重复退出上有一定的帮助。Go语言中使用goto
语句能简化一些代码的实现过程。 例如双层嵌套的for循环要退出时:
1 func gotoDemo1() { 2 var breakFlag bool 3 for i := 0; i < 10; i++ { 4 for j := 0; j < 10; j++ { 5 if j == 2 { 6 // 设置退出标签 7 breakFlag = true 8 break 9 } 10 fmt.Printf("%v-%v\n", i, j) 11 } 12 // 外层for循环判断 13 if breakFlag { 14 break 15 } 16 } 17 }
使用goto
语句能简化代码:
1 func gotoDemo2() { 2 for i := 0; i < 10; i++ { 3 for j := 0; j < 10; j++ { 4 if j == 2 { 5 // 设置退出标签 6 goto breakTag 7 } 8 fmt.Printf("%v-%v\n", i, j) 9 } 10 } 11 return 12 // 标签 13 breakTag: 14 fmt.Println("结束for循环") 15 }
break(跳出循环)
break
语句可以结束for
、switch
和select
的代码块。
break
语句还可以在语句后面添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的for
、switch
和 select
的代码块上。 举个例子:
1 func breakDemo1() { 2 BREAKDEMO1: 3 for i := 0; i < 10; i++ { 4 for j := 0; j < 10; j++ { 5 if j == 2 { 6 break BREAKDEMO1 7 } 8 fmt.Printf("%v-%v\n", i, j) 9 } 10 } 11 fmt.Println("...") 12 }
continue(继续下次循环)
continue
语句可以结束当前循环,开始下一次的循环迭代过程,仅限在for
循环内使用。
在 continue
语句后添加标签时,表示开始标签对应的循环。例如:
1 func continueDemo() { 2 forloop1: 3 for i := 0; i < 5; i++ { 4 // forloop2: 5 for j := 0; j < 5; j++ { 6 if i == 2 && j == 2 { 7 continue forloop1 8 } 9 fmt.Printf("%v-%v\n", i, j) 10 } 11 } 12 }