The glob engine is complete, so put it to work: filter a list of paths by a single pattern, the way a shell or a build tool expands a glob. This capstone exercises the double-star, a star, and a class together against real-looking paths.
Filter a list of paths by a glob pattern, keeping exactly the matches.
This is what a glob is for: expanding a pattern against a set of paths. Filter
is a three-line loop over Match, but the pattern it runs makes every earlier
lesson pull its weight at once. src/**/*.go anchors at src, spans any number of
directories with the double-star, and selects the .go files with a single star in
the final segment - so it keeps src/main.go, src/a/b/util.go, and
src/data/model.go, while rejecting a doc file, a top-level main.go outside
src, and a .txt file at the right depth but the wrong extension.
The second check shows the pieces still compose in any combination: a class in
the last segment, [a-c]*.go, nested under a double-star, matches bar.go (starts
with b) but not zed.go (starts with z). One small Match function, and the
whole POSIX-and-git pattern language falls out of it. Next, the other public entry
point gets its finale.
func Filter(pattern string, paths []string) []string {var out []stringfor _, p := range paths {if Match(pattern, p) { out = append(out, p) }}return out}