go 循环语句

发布时间 2023-12-05 11:10:51作者: HHMLXL
package main

import "fmt"

func main() {
    // 循环语句
    // for init; condition; post {}
    // for condition {}

    // for 循环的 range 格式可以对 slice、map、数组、字符串等进行迭代循环。格式如下:
    // for k, v := range oldmap{
    //     newmap[k] = v
    // }

    // break, continue, goto

    for a:=5; a > 0; a-- {
        fmt.Println(a)
    }
    a := 5
    for a > 0{
        a--
        fmt.Println(a)
    }

    // goto
    for x := 0; x < 10; x++ {
        for y := 0; y < 10; y++ {
            if y == 2 {
                // 跳转到标签
                goto breakHere
            }
        }
    }
    // 手动返回, 避免执行进入标签
    return
    // 标签
breakHere:
    fmt.Println("done")
}