Created
January 25, 2023 16:14
-
-
Save mgnisia/be5f3ae3f645cede0049c375aa3d5459 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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