Reputation: 14978
I am using ioutil
for iterating a folder:
existingFiles, err := ioutil.ReadDir(indexPath)
I want to get the count of files if they are of type .txt
. How can I do that without looping? is there any way to pass the pattern?
Upvotes: 2
Views: 418
Reputation: 34282
You can use filepath.Glob()
:
pattern := filepath.Join(indexPath, "*.txt")
files, err := filepath.Glob(pattern)
if err == nil {
fmt.Printf("Found %d files.\n", len(files))
}
Upvotes: 5