Skip to content

Instantly share code, notes, and snippets.

@adsr303
Last active February 24, 2024 19:08
Show Gist options
  • Save adsr303/0e561001ba20284a8c07b69d2715ef08 to your computer and use it in GitHub Desktop.
Save adsr303/0e561001ba20284a8c07b69d2715ef08 to your computer and use it in GitHub Desktop.
Minimal example of Go 1.22 experimental rangefunc
package main
import "fmt"
func minimalIncomplete(yield func(int, string) bool) {
for i := range 9 {
// If yield returns false because of break/return and we don't check,
// the calling range loop will panic.
yield(i, fmt.Sprintf("==%d==", i))
}
}
func minimal(yield func(int, string) bool) {
for i := range 9 {
if !yield(i, fmt.Sprintf("==%d==", i)) {
return
}
}
}
func main() {
fmt.Println("Minimal without break handling:")
for i, x := range minimalIncomplete {
fmt.Println(i, x)
}
fmt.Println("Minimal:")
for i, x := range minimal {
if i < 3 {
continue
}
if i > 6 {
break
}
fmt.Println(i, x)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment