Extract from collection and build iterators.
go get github.com/miyamo2/xtract
Important
If your Go project is Go 1.23 or higher, this section is not necessary.
Also, if Go1.21 or lower, you will need to update to Go1.22.
go env -w GOEXPERIMENT=rangefunc
s := []string{"gopher", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByValue(func(v string) bool { return len(v) < 9 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: gopher
//iterator
s := []string{"gopher", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKey(func(i int) bool { return i > 0 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: gopher
//iterator
s := []string{"gopher", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKeyAndValue(func(i int, v string) bool { return i > 1 && len(v) > 6 })
for v := range xt.Values()
fmt.Println(v)
}
// Output: range over func
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByValue(func(v string) bool { return len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: gopher
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKey(func(k string) bool { return strings.Contains(k, " ") })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
m := map[string]string{"language": "gopher", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKeyAndValue(func(k, v string) bool { return strings.Contains(k, "e") && len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: gopher
Feel free to open a PR or an Issue.
xtract released under the MIT License