Finder
is a simple Go package for searching files and directories based on patterns in a specified directory. It also supports excluding certain files or directories and provides flexible configuration options for the search.
If you are using Go modules, you can add this package to your project by running:
go get -u github.com/gouef/finder
package main
import (
"fmt"
"github.com/gouef/finder"
)
func main() {
// Search for all files in the current directory and its subdirectories
files := finder.New().In(".").FindFiles("*.go").Get()
// Print the found files
for _, file := range files {
fmt.Println(file.Name())
}
}
You can search for files or directories by specifying patterns using FindFiles
or FindDirectories
:
// Search for .txt files in the current directory and subdirectories
files := finder.New().In(".").FindFiles("*.txt").Get()
You can exclude specific files or directories from the search results using the Exclude
function:
// Search for .txt files, but exclude "test1.txt"
files := finder.New().In(".").FindFiles("*.txt").Exclude("test1.txt").Get()
If you want to search for directories instead of files:
// Search for directories in the current directory and subdirectories
dirs := finder.New().In(".").FindDirectories("*").Get()
New()
– Creates a new instance of Finder.In(dirs ...string)
– Specifies the directories to search in.Find(patterns ...string)
– Searches for both files and directories based on the given patterns.FindFiles(patterns ...string)
– Searches only for files matching the given patterns.FindDirectories(patterns ...string)
– Searches only for directories matching the given patterns.Exclude(patterns ...string)
– Excludes files and directories matching the given patterns from the search results.Get()
– Retrieves the search results.
package main
import (
"fmt"
"github.com/gouef/finder"
)
func main() {
// Search for .go files and exclude "main.go"
files := finder.FindFiles("*.go").In(".").Exclude("main.go").Get()
// Print the found files
for _, file := range files {
fmt.Println(file.Name())
}
}
Extending filepath.Match
, but yout can use multiple patterns.
package main
import (
"fmt"
"github.com/gouef/finder"
)
func main() {
files := finder.FindFiles("*.go").In(".").Exclude("main.go").Get()
}
Read Contributing
Click above to join our community on Discord!