Home > Back-end >  How to efficiently find all the files matching a particular suffix in all the directories?
How to efficiently find all the files matching a particular suffix in all the directories?

Time:12-31

I need to recursively find all the files in a directory which can have lot of sub-directories too. I was wondering if there is any way it can only give me *.json files only? I have below method which does the job and gives me all files recursively in all the directories but I am not sure whether I am doing it right by using HasSuffix here. I am new to golang so I not sure if there is any better or efficient way to do this.

func WalkDir(root string) ([]string, error) {
    var files []string
    err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
        if !info.IsDir() && strings.HasSuffix(path, "json") {
            files = append(files, path)
        }
        return nil
    })
    return files, err
}

Also how can I modify above method so that it can give me all the files matching *.json or *.txt suffix. Basically it should be able to work with suffix array and give me all files matching that. What is the efficient way to do this?

CodePudding user response:

You probably want to ensure the dot (.) is present before the file suffix too.

Also as noted by @maxm, WalkDir is preferred according to the docs:

Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory.

To check for a match of multiple suffixes you can try:

func WalkDir(root string, exts []string) ([]string, error) {
    var files []string
    err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
        if d.IsDir() {
            return nil
        }

        for _, s := range exts {
            if strings.HasSuffix(path, "." s) {
                files = append(files, path)
                return nil
            }
        }

        return nil
    })
    return files, err
}

And to use:

files, err := WalkDir("/home", []string{"json", "txt"})
  •  Tags:  
  • go
  • Related