go slice for loop code example
Example 1: golang iterate through slice
var slice = []string{"Apple", "Orange", "Kiwi"}
for index, sliceItem := range slice { // You can omit index or sliceItem with a _
fmt.Println("Index in slice:", index)
fmt.Println("Fruit:", sliceItem)
}
Example 2: go iterate over slice
nums := []int{2, 3, 4}
sum := 0
for _, num := range nums {
sum += num
}
fmt.Println("sum:", sum)