Go语言中最常用的流程控制有iffor,而switchgoto主要是为了简化代码,降低重复代码而生的结构,属于扩展类的流程控制。

if else(分支结构)

  • if条件判断的基本写法

    1
    2
    3
    4
    5
    6
    7
    if 表达式1 {
    分支1
    } else if 表达式2 {
    分支2
    } else{
    分支3
    }

    Go语言规定与if匹配的左括号{必须与if和表达式放在同一行,{放在其他位置会触发编译错误。 同理,与else匹配的{也必须与else写在同一行,else也必须与上一个ifelse if右边的大括号在同一行。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    //多条件判断
    size := 19
    if size > 35 {
    fmt.Println("大")
    } else if size > 18 {
    fmt.Println("中")
    }else {
    fmt.Println("小")
    }
  • if条件判断的特殊写法

    可以在 if 表达式之前添加一个执行语句,再根据变量值进行判断,定义的age为局部变量,执行完成后即销毁,节省内存开销

    1
    2
    3
    4
    5
    6
    //if条件判断的特殊写法
    if age := 19; i > 18 {
    fmt.Println( age, "岁:恭喜你成年了")
    }else {
    fmt.Println( age, "岁:回家写作业去")
    }

for(循环结构)

1
2
3
for 初始语句;条件表达式;结束语句{
循环体语句
}

​ 基本格式

1
2
3
4
//基本格式
for i := 0; i < 10; i++ {
fmt.Println(i)
}

​ 变种1(表达式1在for外面定义)

1
2
3
4
5
//变种1
m := 10
for ; m < 15; m++ {
fmt.Println(m)
}

​ 变种2(表达式3写在for内部)

1
2
3
4
5
6
//变种2
n := 20
for ; n < 27; {
fmt.Println(n)
n++
}

​ 死循环

1
2
3
4
//无限循环
for {
fmt.Println(time.ANSIC)
}

for range循环(类似foreach)

1
2
3
4
5
// for range循环
str := "Hello 世界"
for index,value := range str{
fmt.Printf("%d : %c \n", index, value)
}

打印九九乘法表

1
2
3
4
5
6
for i := 1; i <= 9;i ++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d * %d = %d\t", i, j, i*j)
}
fmt.Println()
}

switch … case

使用switch语句可以方便的对大量的值进行条件判断,每个switch只能有一个default分支。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
a := 5
switch a {
case 1:
fmt.Println("大拇指")
case 2:
fmt.Println("食指")
case 3:
fmt.Println("中指")
case 4:
fmt.Println("无名指")
case 5:
fmt.Println("小指")
default:
fmt.Println("无效数字")
}

变种一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//定义switch局部变量
switch a := 4; a {
case 1:
fmt.Println("大拇指")
case 2:
fmt.Println("食指")
case 3:
fmt.Println("中指")
case 4:
fmt.Println("无名指")
case 5:
fmt.Println("小指")
default:
fmt.Println("无效数字")
}

变种二

一个分支可以有多个值,多个case值中间使用英文逗号分隔。

1
2
3
4
5
6
7
//定义一个case多个判断条件
switch a := 2; a {
case 1, 2, 3, 4, 5:
fmt.Println("手指")
default:
fmt.Println("无效数字")
}

变种三

1
2
3
4
5
6
7
8
9
10
11
12
//case加表达式,判断条件放到case,switch外声明变量
age := 30
switch {
case age < 25:
fmt.Println("好好学习吧")
case age >= 25 && age < 35:
fmt.Println("人到中年")
case age >= 35 && age < 60:
fmt.Println("好好享受吧")
default:
fmt.Println("活着就好")
}

变种四

1
2
3
4
5
6
7
8
9
10
11
//case加表达式,switch局部变量
switch age := 30; {
case age < 25:
fmt.Println("好好学习吧")
case age >= 25 && age < 35:
fmt.Println("人到中年")
case age >= 35 && age < 60:
fmt.Println("好好享受吧")
default:
fmt.Println("活着就好")
}

fallthrough语法可以执行满足条件的case的下一个case,是为了兼容C语言中的case设计的。

1
2
3
4
5
6
7
8
9
10
11
12
//fallthrough
switch a := 2; a {
case 1:
fmt.Println("我是1")
case 2:
fmt.Println("我是2")
fallthrough
case 3:
fmt.Println("我是3")
default:
fmt.Println("罢工不干了")
}

goto(跳转到指定标签)

goto语句通过标签进行代码间的无条件跳转。goto语句可以在快速跳出循环、避免重复退出上有一定的帮助。Go语言中使用goto语句能简化一些代码的实现过程。

常用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//常用break跳出双层for循环写法
flag := false
for i := 0; i < 10; i++ {
for a := 'A'; a < 'Z'; a++ {
if a == 'C' {
flag = true
break
}
fmt.Printf("%d-%c\n", i, a)
}
if flag {
break
}
}

goto + lable跳出循环

1
2
3
4
5
6
7
8
9
10
11
12
//goto + TABLE跳出for循环
for i := 0; i < 10; i++ {
for a := 'A'; a < 'Z'; a++ {
if a == 'C' {
goto TO
}
fmt.Printf("%d-%c\n", i, a)
}
}

TO:
fmt.Printf("GameOver\n")

break(跳出循环)

reak语句可以结束forswitchselect的代码块。

break语句还可以在语句后面添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的forswitchselect的代码块上

1
2
3
4
5
6
7
8
9
10
//break + TABLE跳出for循环
Test:
for i := 0; i < 10; i++ {
for a := 'A'; a < 'Z'; a++ {
if a == 'C' {
break Test
}
fmt.Printf("%d-%c\n", i, a)
}
}

continue(继续下次循环)

continue语句可以结束当前循环,开始下一次的循环迭代过程,仅限在for循环内使用。

continue语句后添加标签时,表示开始标签对应的循环。

1
2
3
4
5
6
7
8
9
10
	//continue + TABLE跳出for循环
Test1:
for i := 0; i < 10; i++ {
for a := 'A'; a < 'Z'; a++ {
if a == 'C' {
continue Test1
}
fmt.Printf("%d-%c\n", i, a)
}
}