I have a directory structure like this:
reports
├── bar
│?? └── 2021-01-25
└── foo
└── 2021-01-25
I am new to go and try to use the Walk function,
Now I have this code
func FilePathWalkDir(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
if info.IsDir() && info.Name() == "pages" || info.Name() == "data" || info.Name() == "logs" {
return filepath.SkipDir
}
files = append(files, path)
}
return nil
})
return files, err
}
And using this like:
func serve(w http.ResponseWriter, r *http.Request) {
lines := []string{}
files, err := FilePathWalkDir("reports/")
if err != nil {
panic(err)
}
for _, file := range files {
lines = append(lines, file)
}
output, err := json.Marshal(lines)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
//fmt.Println(string(jsonData))
fmt.Fprintf(w, string(output))
}
And I get this
"reports/"
"reports/bar"
"reports/bar/2021-01-25"
"reports/foo"
"reports/foo/2021-01-25"
But want this:
"reports/bar/2021-01-25"
"reports/foo/2021-01-25"
But I only want to return the last bit (reports/foo/2021-01-27) for each directory - with the dates, and it could be many different dates, like it could be many different project names (foo, bar, barfoo, foobar etc).
Could anyone point me in the right direction?
question from:
https://stackoverflow.com/questions/65946292/how-to-return-a-directory-structure-as-a-complete-tree 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…