How to iterate over the elements of a slice in Golang?

In Golang, you can iterate over the elements of a slice using a for loop.

Here is an example:

package main import "fmt" func main() { slice := []int{1, 2, 3, 4, 5} // Using range to iterate over the elements of the slice for index, value := range slice { fmt.Printf("Index: %d, Value: %d\n", index, value) } }

In this example, the range keyword is used to iterate over the elements of the slice. The loop will iterate over each element and assign the index and value to the index and value variables in each iteration.

The output will be:

Index: 0, Value: 1 Index: 1, Value: 2 Index: 2, Value: 3 Index: 3, Value: 4 Index: 4, Value: 5

You can use this method to iterate over the elements of the slice and perform any desired operations on them.