Find files in directory

please, help,
task to find all the files of a certain extension in one directory, how I can do this?

For example so:

package main

import (
	"io/ioutil"
	"log"
	"os"
	"strings"
)

func main() {

	files, err := ioutil.ReadDir("./")
	if err != nil {
		log.Fatal(err)
	}

	target := []os.FileInfo{}

	for _, f := range files {
		if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") {
			target = append(target, f)
			log.Println(f.Name())
		}
	}

}

But it would be better if you provided your code first.

4 Likes

thanks man , it work for me

You can also use filepath.Walk, especially for very large directories.
:slightly_smiling_face:

2 Likes

The linked documentation says it were inefficient for large directories…

1 Like

I used for large directories (on Linux) and I didn’t feel a lack of performance. Perhaps could be ineficient for realy very large directories…

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.