流程控制

if判断 else

     age := 16
     country :="中国"
     if age <18&& country=="中国" {
         fmt.Println("未成年")
     }else{
         fmt.Println("已成年")
     }

//输出 未成年。

也可以嵌套

     age := 19
     country :="中国"
     if age <18{
         if country =="中国"{
            fmt.Println("未成年")
         }

     }else if age==18{
         fmt.Println("成年")
     } else{
          fmt.Println("已成年")
     }
//输出 已成年

for循环

    for i:= 0;i<3;i++{
        fmt.Println(i)
    }
    //输出
    //0
    //1
    //2

go时没有while死循环的,因为可以通过for来实现

for  {
    //todo 满足了在跳出去,break等
}

嵌套循环 99乘法表

    for y:=1 ;y<=9;y++{
        for x :=1 ; x<=y;x++{
            fmt.Printf("%d*%d=%d \t",y,x,x*y)
        }
        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     

遍历 range

参考文档:range

    m := map[string]string{
        "apple":  "aba",
        "banana": "abb",
        "orange": "abc",
    }
    for key, val := range m {
        fmt.Println(key, val)
    }
    输出:
apple aba
banana abb
orange abc

字符串转义输出
由于中文是占3个字符,所以直接遍历字符串会出现会出现中文乱码,所以用rune转
for循环如果只接收一个值,则该值为key或者index如果是同道则是element

    name := "imooc go体系课"
    names := []rune(name)

    for key := range names {
        //fmt.Println(name[key])
        fmt.Printf("%c \n\r", names[key])
    }

循环的退出

continue:跳出一次
break:退出本层循环

    for i := 0; i < 10; i++ {
        if i == 2 {
            continue
        } else if i == 5 {
            break
        }
        fmt.Println(i)
    }

0
1       
3       
4    

continue 跳出代码块

OuterLoop:
    for i := 0; i < 2; i++ {
        for j := 0; j < 5; j++ {
            switch j {
            case 2:
                fmt.Println(i, j)
                continue OuterLoop
            }
        }
    }
}

fmt.Println("跳出来了")

0 2
1 2     
跳出来了

break 跳出代码块


OuterLoop:
    for i := 0; i < 2; i++ {
        for j := 0; j < 5; j++ {
            switch j {
            case 2:
                fmt.Println(i, j)
                break OuterLoop
            case 3:
                fmt.Println(i, j)
                break OuterLoop
            }
        }
    }
    fmt.Println("跳出来了")

0 2
跳出来了

goto 平时慎用,跳转到指定标签处,代码逻辑会不容易看清。

    for i := 0; i < 2; i++ {
        for j := 0; j < 5; j++ {
            switch j {
            case 2:
                fmt.Println(i, j)
                goto OuterLoop
            }
        }
    }

OuterLoop:

switch 分支管理

https://www.topgoer.cn/docs/golang/chapter04-2

常规用法

    age := 16
    switch age {
    case 16:
        fmt.Println("16岁了哦mmmm")
    case 18:
        fmt.Println("完犊子了,成年了")
    default:
        fmt.Println("其他的情况了")
    }

多key进同一个流程

    age := 80
    switch age {
    case 18, 80:
        fmt.Println("十八岁和八十岁的生日要好好过一下啦") //18和80都能进入该分支
    case 20:
        fmt.Println("完犊子了,成年了")
    default:
        fmt.Println("其他的情况了")
    }

灵活的表达式。。

    age := 30
    name := "李雷"
    switch {
    case name == "李雷" && age == 30:
        fmt.Println("李雷三十而立要出事")
    case age == 30:
        fmt.Println("30而立了")
    default:
        fmt.Println("其他的情况了")
    }

代码输出‘李雷三十而立要出事’,如果name不等于李雷而age还是30时,则显‘30而立了’。而如果都不满足则显示 default的内容分支。。

文档更新时间: 2023-04-02 01:07   作者:young