条件
if condition1 {
// do something
} else if condition2 {
// do something else
} else {
// catch-all or default
}
分支
switch num1 {
case 98, 99:
fmt.Println("It's equal to 98")
case 100:
fmt.Println("It's equal to 100")
default:
fmt.Println("It's not equal to 98 or 100")
}
自动 break
,不想自动跳出,则使用 fallthrough
switch i {
case 0: fallthrough
case 1:
f () // 当 i == 0 时函数也会被调用
}
支持条件:
switch {
case i < 0:
f1()
case i == 0:
f2()
case i > 0:
f3()
}
支持初始化:
switch a, b := x[i], y[j]; {
case a < b: t = -1
case a == b: t = 0
case a > b: t = 1
}
for 循环
for i := 0; i < 5; i++ {
fmt.Printf("This is the %d iteration\n", i)
}
死循环:
for { }
迭代:
for pos, char := range str {
...
}
跳转
支持 goto,别用。