Go 结构化编程 | 青训营笔记

63 阅读2分钟

这是我参与「第五届青训营 」伴学笔记创作活动的第 2 天

1. if 分支控制

age := 20
var res string

if age < 18 {
    res = "童年"
} else if age >= 18 && age < 30 {
    res = "中年"
} else 
{
    res = "老年"
}
println(res)
  • if 后的条件表达式不需要括号
  • 语句块外的大括号不可省略
  • if 后的、else 前的 大括号的不可换行
  • if 后的表达式必须为布尔类型,所以不会有 == 写为 = 的困扰

Go 可以在 if 条件表达式处定义变量

if a := 1; a >= 1 {
    fmt.Println(a)
}

分号前面可以是任意语句,分号后面是 if 的条件表达式。

if text, err := ioutil.ReadFile("test.txt"); err == nil {
    fmt.Printf("%s", text)
} else {
    fmt.Println(err)
    // open test.txt: The system cannot find the file specified.
}

2. switch 分支控制

switch age {
case 1, 3, 5:
    fmt.Println("0")
case 2, 4, 6, 9 + 1:
    fmt.Println("1")
default:
    fmt.Println("default")
}
  • switch 后接一个表达式、每个 case 后可接一个或多个逗号隔开的表达式,必须保证这些表达式的值的类型一致。
  • Go 的 case 块中不需要 break,会自动为每个 case 块的最后添加。
  • 但 Go 的 case 后的表达式可以使用逗号来分隔多个表达式

switch 后可不接表达式,此时匹配第一个值为 true 的 case 表达式:

age := 17
var res string
switch {
case age < 18:
    res = "童年1"
case age < 18:
    res = "童年2"
case age >= 18 && age < 30:
    res = "中年"
default:
    res = "老年"
}
println(res)

case 穿透:在 case 块的最后加关键字 fallthrough,可以去除自动生成的 break

age = 3
switch age {
case 1, 3, 5:
    fmt.Println("0")
    fallthrough // 向下穿透一个 case
case 2, 4, 6, 9 + 1:
    fmt.Println("1")
    // 默认会添加一个 break
default:
    fmt.Println("default")
}

用于检查类型

var x interface{}
x = age

switch x.(type) {
case int:
    fmt.Println("int")
case int32:
    fmt.Println("int32")
case float32:
    fmt.Println("float32")
default:
    fmt.Println("all not")
}

3. for 循环控制

for 同时担任了 forwhile 的作用:

for i := 0; i < 10; i++ {
    fmt.Println(i)
}


i := 0
for i < 1 {
    fmt.Println(i)
    i++
}

无条件循环

i := 0
for {
    if i >= 10 {
        break
    }
    fmt.Println(i)
    i++
}

增强 for 循环:for-range 第一个是下标,第二个是元素

arr := []int{1, 2, 3, 4}
for index, item := range arr {
    fmt.Println(index, item)
}
for index := range arr {
    fmt.Println(index)
}

// 对于字符串是按字符来遍历的
str := "hello, 世界"
for _, item := range str {
    fmt.Printf("%c ", item) // h e l l o ,   世 界
}
fmt.Println()
for index := range str {
    fmt.Printf("%d ", index) // 0 1 2 3 4 5 6 7 10
}

3.1. break continue

多层嵌套循环时,可以使用标签来指明跳出哪层循环

A:
for i := 0; i < 3; i++ {
    for j := 0; j < 10; j++ {
        fmt.Println("j", j)
        if j == 2 {
            break A
        }
    }
    fmt.Println("i", i)
}
// j 0
// j 1
// j 2

B:
for i := 0; i < 3; i++ {
    for j := 0; j < 10; j++ {
        fmt.Println("j", j)
        if j == 2 {
            continue B
        }
    }
    fmt.Println("i", i)
}
// j 0
// j 1
// j 2
// j 0
// j 1
// j 2
// j 0
// j 1
// j 2

3.2. 老惯例:九九乘法表

for i := 1; i <= 9; i++ {
    for j := 1; j <= i; j++ {
        fmt.Printf("%d*%d=%d ", i, j, i*j)
    }
    fmt.Println()
}
/*
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
*/