Skip to content

Instantly share code, notes, and snippets.

@kitlabcode
Created April 20, 2023 15:38
Show Gist options
  • Select an option

  • Save kitlabcode/92a96ca18f0071b18702df9aacbda565 to your computer and use it in GitHub Desktop.

Select an option

Save kitlabcode/92a96ca18f0071b18702df9aacbda565 to your computer and use it in GitHub Desktop.
When loop meets a condition start the loop over again.
package main
import "fmt"
func main() {
slice := []int{1, 2, 3, 4, 5}
var count int
for i := 0; i < len(slice); i++ {
fmt.Println(slice[i])
count++
if count == 2 {
i = -1 // restart from the beginning
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment