packagemainimport"fmt"funcmain(){personAge:=map[string]int{"Amy":1,"Daming":2,"Lily":3,}ifvalue,ok:=personAge["Amy"];ok==true{fmt.Println("Amy’s value is ",value)}else{fmt.Println("Amy is not here.")}}
k is :1
k is :2
k is :3
k is :4
k is :5
k is :6
k is :7
k is :8
k is :9
k is :10
# 多个变量控制输出结果1 * 10=102 * 9=183 * 8=244 * 7=285 * 6=306 * 5=307 * 4=288 * 3=249 * 2=1810 * 1=10
packagemainimport"fmt"funcmain(){// continue完成了只输出k>5的取值
fork:=1;k<=10;k++{ifk<=5{continue}fmt.Printf("k is :%d\n",k)}// break让i=10时退出for循环
fori:=1;;i=i+1{ifi==10{fmt.Printf(" is :%d\n",i)break}fmt.Printf("i is :%d\n",i)}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
本例中continue的效果
k is :6
k is :7
k is :8
k is :9
k is :10
本例中break的效果
i is :1
i is :2
i is :3
i is :4
i is :5
i is :6
i is :7
i is :8
i is :9
i is :10
🐵goto跳转
goto使得程序直接到指定行执行,不过不建议常用,goto容易造成程序流程混乱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
packagemainimport"fmt"funcmain(){fori:=1;i<=10;i=i+1{ifi%2==0{gotoFind}fmt.Printf("i is :%d\n",i)}Find:fmt.Println("偶数出现")}