Skip to content

Instantly share code, notes, and snippets.

@damlys
Last active January 9, 2023 14:35
Show Gist options
  • Save damlys/c12d4c5b4f34ab594241a734b4a39fed to your computer and use it in GitHub Desktop.
Save damlys/c12d4c5b4f34ab594241a734b4a39fed to your computer and use it in GitHub Desktop.
Golang loops: for, for-in, for-each, for-of, infinity, while, do-while, repeat-until
package main
import "fmt"
func main() {
directions := [...]string{"up", "right", "down", "left"}
for i := 0; i < len(directions); i++ {
fmt.Printf("for loop [%d]%s\n", i, directions[i])
}
for i := range directions {
fmt.Printf("for-in loop [%d]%s\n", i, directions[i])
}
for i, v := range directions {
fmt.Printf("for-each loop [%d]%s\n", i, v)
}
for _, v := range directions {
fmt.Printf("for-of loop [ ]%s\n", v)
}
for {
fmt.Println("infinity loop", 1)
break
}
n := 1
for n <= 3 {
fmt.Println("while loop", n)
n++
}
n = 1
for do := true; do; do = n <= 3 {
fmt.Println("do-while loop", n)
n++
}
n = 1
for end := false; !end; end = n > 3 {
fmt.Println("repeat-until loop", n)
n++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment