I'm using the next code to looking for some files by its extension.
But I've some doubts:
1) I've to use a global variable to store the values got in search().
Could be used a local variable to return in walkDir()?
2) Is there any way to skip a full subdirectory? The only way that
I've found is to checking the path to see if the subdirectory to skip
is in the path but it has to checked for each path so its performance
is not very good
By example, to checking hidden directories:
./foo/
./bar/
./.git/ // it would be found
./.git/a.txt // it would be found
./.git/b.txt // it would be found
./doc/
But I would want that, when ".git" is found, then the next path were
"doc"
// * * *
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
var paths = make([]string, 0)
func walkDir(path string) {
errors := make(chan error)
done := make(chan bool)
// Error handler
go func() {
for err := range errors {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err) // TODO: send to a file
}
}
done <- true
}()
//fmt.Printf(" + Checking: %q...\n", path)
filepath.Walk(path, search(errors))
close(errors)
<-done
fmt.Println()
}
// Search files with ".go~" extension.
func search(errors chan error) filepath.WalkFunc {
// Implements "filepath.WalkFunc".
return func(path string, info os.FileInfo, err error) error {
if err != nil {
errors <- err
return nil
}
if info.IsDir() {
fmt.Printf(" + checking: %q\n", path)
}
// Skip directories ang hidden files
if info.IsDir() || filepath.HasPrefix(info.Name(), ".") {
return nil
}
fileExt := strings.ToLower(filepath.Ext(info.Name()))
if fileExt == ".go~" {
paths = append(paths, path)
}
return nil
}
}
func main() {
walkDir("/home/neo/go/src/
github.com/kless/GoWizard")
fmt.Println(paths)
}
// * * *