- if
- Go语言中if 判断不加括号
- Go 没有 条件运算符 (a>b ? a:b)
if x:=f(); x<y{ //if后面可以加初始化语句,但作用域在if里面包括else if
return x
}else if x>z{
return z
}else{
return y
}
- switch
- go里面switch 条件表达式不像C限制与整数,可以是任意支持相等运算类型变量
switch i:= "k"; i {
case "y", "Y":
fmt.Println("yes")
//fallthrough 加了fallthrough 跳过本case运行下一个case,且不判断下一个case
//break
case "n", "N":
fmt.Println("No")
default:
fmt.Println("1111")
}
- switch省去条件判断就是多个if else语句
- for
for 3种方式
for init; condition; post{}
for condition {} //类似于while
for {} 类似于 while(1)
//访问map
for key, value := range map{}
for key := range map{}
//访问数组
for index, value := range arry{}
for index := range arry{}
for _, value := range arry{}
//访问切片
for index, value := range slice {}
- 标签和跳转(lable)
Lable: Statement
- goto : 用于函数内部跳转,需要配合label
goto Lable
-
goto只能在同级作用域或跳到上一层,且不能跳过变量声明语句
-
break 与 continue,加lable都只能用于在同一函数内
func main() {
L1://跳出到这一层
for i:=0; ;i++{
for j:=0 ; ; j++{
if i>=5{
fmt.Println("i = ",i)
break L1
}
if j>10{
fmt.Println("j = ",j)
break
}
}
}
fmt.Println("到这里这里")
}