Golang: Iterate Slice
Iterate Slice or Array
for i, v := range slice {body}
-
iterate slice, where i is current index and v the value.
If a variable is not used, name it
_
to stop compiler from complaining. e.g.for _, v := range slice {body}
. The_
is called blank identifier.
package main import "fmt" func main() { var s = []int{9, 2, 8, 61} for i, x := range s { fmt.Println(i, x) } } // 0 9 // 1 2 // 2 8 // 3 61
Golang, array and slice
- Golang: Array
- Golang: Slice
- Golang: Slice of Slice
- Golang: Append to Slice
- Golang: Cut Slice (Delete Elements)
- Golang: Copy Slice
- Golang: Clear Slice
- Golang: Nested Slice
- Golang: Slice of Strings to String
- Golang: Iterate Slice
- Golang: Convert Value to String
- Golang: Convert Array to Slice
- Golang: Print Slice, Array