-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainFileNew.go
53 lines (41 loc) · 1.08 KB
/
mainFileNew.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"errors"
"fmt"
"io/ioutil"
)
// 递归 实现文件夹遍历: 涉及到栈的实现
func GetAllX(path string, files []string, level int) ([]string, error) {
levelstr := ""
if level == 1{
levelstr = "+"
} else {
for ; level > 1; level--{
levelstr += "|--"
}
levelstr +="+"
}
read, err := ioutil.ReadDir(path) //读取文件夹
if err != nil{
return files, errors.New("文件夹不可读取")
}
for _, fi := range read { // 循环每个文件或者文件夹
if fi.IsDir(){ // 判断是否是文件夹
fullDir := path+"\\"+fi.Name() //构造新的路径
files = append(files, levelstr+fullDir) //追加路劲
files,_ = GetAllX(fullDir,files, level+1) //文件夹递归
}else {
fullDir := path+"\\"+fi.Name() //构造新的路径
files = append(files, levelstr+fullDir) //追加路劲
}
}
return files,nil
}
func main4() {
path := "D:\\WorkDir" // 路径
files := []string{} // 数组字符串
files, _ = GetAllX(path, files,1) // 抓取所有文件
for i:= 0; i< len(files);i++{ //打印
fmt.Println(files[i])
}
}