Golang入门四:流程控制

条件语句

a := 3
if a < 5 {
    fmt.Println("a 小于 5...")
} else {
}

选择语句

switch i {
    case 0:
        fmt.Println("0")
    case 1:
        fmt.Println("1")
    case 2:
        fallthrough
    case 3:
        fmt.Println("3")
    case 4, 5, 6:
        fmt.Println("4, 5, 6")
    default:
        fmt.Println("Default")
}
switch {
    case 0 <= num && num <= 3:
        fmt.Println("0-3")
    case 4 <= num && num <= 6:
        fmt.Println("4-6")
    case 7 <= num && num <= 9:
        fmt.Println("7-9")
}

循环语句

for i := 0; i < 10; i++ {
    fmt.Println("Hello ", i)
}
// 无限循环
sum := 0
for {
    sum++
    if sum > 100 {
        break
    }
}

跳转语句goto

func myFunc() {
    i := 0
    HERE:
    fmt.Println(i)
    i++
    if i < 10 {
        goto HERE
    }
}

你可能感兴趣的:(Golang入门四:流程控制)