How to iterate over a slice in reverse order in Go?
Hello everyone! I just started learning Go and came across this task: I need to iterate over slice elements from end to beginning. In Python I'm used to just writing `for i in reversed(arr)`, in JavaScript — `.reverse().forEach(...)`. But in Go, as far as I understand, there is no built-in function for reverse iteration.
Here's an example of the code I have:
```go
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
// Want to output: 5, 4, 3, 2, 1
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i]) // outputs 1,2,3,4,5
}
}
```
I tried changing the loop condition: `for i := len(numbers); i > 0; i--`, but this causes a panic (panic: runtime error: index out of range [5] with length 5). Then I tried `for i := len(numbers) - 1; i > 0; i--`, but
No answers yet.
Log in to leave an answer