break外层循环
func main() { FirstLoop: for i := 0; i < 10; i++ { for j := 0; j < 10; j++ { fmt.Printf("i=%d, j=%d\r\n", i, j) if j == 5 { break FirstLoop } } } fmt.Println("the end") }
range读取slice
func main() { array := [3]int32{1, 2} aslice = array[0:2] for i, v := range aslice { fmt.Printf("[%d]=%d ", i, v) } }
range读取map
for key, value := range map{ fmt.Println("map's key:", key, " val:", value) }
append slice
func test_slice_append1() { array := [2]int32{1, 2} //array={1,2} aslice := array[0:2] //aslice={1,2} aslice = append(aslice, 3) fmt.Println(array) //array={1,2} fmt.Println(aslice) //aslice={1,2,3} aslice[0] = 4 fmt.Println(array) //array={1,2} fmt.Println(aslice) //aslice={4,2,3} } func test_slice_append2() { array := [3]int32{1, 2} //array={1,2,0} aslice := array[0:2] //aslice={1,2} aslice = append(aslice, 3) fmt.Println(array) //array={1,2,3} fmt.Println(aslice) //aslice={1,2,3} aslice[0] = 4 fmt.Println(array) //array={4,2,3} fmt.Println(aslice) //aslice={4,2,3} }
switch case跟C由一点区别,没有break
i := 10 switch i { case 1: fmt.Println("i is equal to 1") case 2, 3, 4: fmt.Println("i is equal to 2, 3 or 4") case 10: fmt.Println("i is equal to 10") default: fmt.Println("All I know is that i is an integer") }
defer很好用,在退出函数时延迟执行,先进后出即按栈方式进行
有疑问加站长微信联系(非本文作者)