Volatil3
Volatil3

Reputation: 14978

How to get file count of a particular extension in Go?

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

Answers (1)

bereal
bereal

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

Related Questions