package main import ( "fmt" "io/ioutil" ) func main() { dirName := "D:\\tmp\\20220314" //fmt.Println(dirName) res := FileList(dirName, 0) for _, file := range res { fmt.Println(file) } } func FileList(dirName string, level int) []string { /* 根据层级读取目录下文件 并返回list */ s := "|--" for i := 0; i < level; i++ { s = "| " + s } var ret []string fileInfo, err := ioutil.ReadDir(dirName) if err != nil { fmt.Println(err) return ret } for _, fi := range fileInfo { fileName := dirName + "/" + fi.Name() fmt.Printf("%s%s\n", s, fileName) if fi.IsDir() { tmpRes := FileList(fileName, level+1) ret = append(ret, tmpRes...) } else { ret = append(ret, fileName) } } return ret }