Skip to content

Instantly share code, notes, and snippets.

@mgnisia
Created January 25, 2023 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgnisia/be5f3ae3f645cede0049c375aa3d5459 to your computer and use it in GitHub Desktop.
Save mgnisia/be5f3ae3f645cede0049c375aa3d5459 to your computer and use it in GitHub Desktop.
// Resource: https://zetcode.com/golang/find-file/
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
var files []string
func VisitFile(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
return nil
}
if info.IsDir() || filepath.Ext(path) != ".txt" {
return nil
}
reg, err2 := regexp.Compile("^[la]")
if err2 != nil {
return err2
}
if reg.MatchString(info.Name()) {
files = append(files, path)
}
return nil
}
func main() {
err := filepath.Walk("some/folder", VisitFile)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment