Skip to content

Instantly share code, notes, and snippets.

@manjeettahkur
Created May 30, 2022 06:37
Show Gist options
  • Save manjeettahkur/f615bcf37ea0adbf9263e091e0c95395 to your computer and use it in GitHub Desktop.
Save manjeettahkur/f615bcf37ea0adbf9263e091e0c95395 to your computer and use it in GitHub Desktop.
Difference between for loop vs for range loop
package main
import (
"fmt"
)
func main() {
s := []int{1, 2}
for i := range s {
fmt.Println(i)
if i > 10 {
break
}
s = append(s, i*2)
}
fmt.Println(s)
package main
import (
"fmt"
)
func main() {
s := []int{1, 2}
for i := 0; i < len(s); i++ {
if i > 10 {
break
}
s = append(s, i*2)
}
fmt.Println(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment