Created
May 30, 2022 06:37
-
-
Save manjeettahkur/f615bcf37ea0adbf9263e091e0c95395 to your computer and use it in GitHub Desktop.
Difference between for loop vs for range loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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