Last active
February 24, 2024 19:08
-
-
Save adsr303/0e561001ba20284a8c07b69d2715ef08 to your computer and use it in GitHub Desktop.
Minimal example of Go 1.22 experimental rangefunc
This file contains 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 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