7.3 for-range
...小于 1 分钟
7.3 for-range
可用for-range
遍历切片和数组
for index, value := range slice {
// ...
}
- value: 只是元素值的拷贝无法修改元素值
- index: 元素索引,可用
slice[index]
形式来修改值
func forSlice() {
s := []int{1, 2, 3}
fmt.Println(s)
for _, v := range s {
v++
}
fmt.Println(s)
for i := range s {
s[i] = 5
}
fmt.Println(s)
}
[1 2 3]
[1 2 3]
[5 5 5]
Powered by Waline v2.15.2