What Is the Difference Between for range and for i in Go

Use for range for reading slices and for i loops when modifying slice elements.

Use for range to iterate over values or indices without manual index management, and use for i := 0; i < len(slice); i++ when you need to modify elements or access the index explicitly.

// Read-only iteration with index and value
for i, v := range slice {
    fmt.Println(i, v)
}

// Modification requires explicit index loop
for i := 0; i < len(slice); i++ {
    slice[i] = slice[i] * 2
}