Created
April 3, 2021 02:04
-
-
Save prasetiyohadi/46b13f86f0df7c2d318596ea6317c278 to your computer and use it in GitHub Desktop.
Go snippet for traversing file path
This file contains 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
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