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?