golang随笔
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很好用,在退出函数时延迟执行,先进后出即按栈方式进行
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。