Skip to content

Instantly share code, notes, and snippets.

@prasetiyohadi
Created April 3, 2021 02:04
Show Gist options
  • Save prasetiyohadi/46b13f86f0df7c2d318596ea6317c278 to your computer and use it in GitHub Desktop.
Save prasetiyohadi/46b13f86f0df7c2d318596ea6317c278 to your computer and use it in GitHub Desktop.
Go snippet for traversing file path
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
)
func main() {
var files []string
root := "testdir"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if matched, _ := regexp.MatchString(`somefile*`, path); !matched {
return nil
}
member := filepath.Base(path)
fmt.Println(member)
files = append(files, member)
return nil
})
if err != nil {
panic(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