In order to jump out of inner loop, and continue with the outer loop, we could either use continue Label
or use break
.
Here's an example from http://www.goinggo.net/2013/11/label-breaks-in-go.html
The original post is using the continue Label
pattern:
guestList := []string{"bill", "jill", "joan"}
arrived := []string{"sally", "jill", "joan"}
CheckList:
for _, guest := range guestList {
for _, person := range arrived {
fmt.Printf("Guest[%s] Person[%s]
", guest, person)
if person == guest {
fmt.Printf("Let %s In
", person)
continue CheckList
}
}
}
The same result could be achieved through using break
, as shown here:
http://play.golang.org/p/0YUjkdxxRE
guestList := []string{"bill", "jill", "joan"}
arrived := []string{"sally", "jill", "joan"}
for _, guest := range guestList {
for _, person := range arrived {
fmt.Printf("Guest[%s] Person[%s]
", guest, person)
if person == guest {
fmt.Printf("Let %s In
", person)
break
}
}
}
Which way is more idiomatic in Go
?