下面这段代码很神奇

openset · · 1458 次点击
openset
Less is more!
下面2段代码输出结果一样 ```go s := []int{1, 2, 3} for i, v := range s { s = append(s, s[i]+3) fmt.Println("i: ", i, "\tv: ", v) } fmt.Println(s) ``` ```go s := make([]int, 3, 100) s[0], s[1], s[2] = 1, 2, 3 for i, v := range s { s = append(s, s[i]+3) fmt.Println("i: ", i, "\tv: ", v) } fmt.Println(s) ``` 运行输出结果: ``` i: 0 v: 1 i: 1 v: 2 i: 2 v: 3 [1 2 3 4 5 6] ```
#5
更多评论
The range expression x is evaluated once before beginning the loop
#2