-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (40 loc) · 749 Bytes
/
main.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
)
func main() {
ReadAndOutputDir("../../..", 3)
}
func ReadAndOutputDir(rootPath string, deep int) {
file, err := os.Open(rootPath)
if err != nil {
fmt.Println("error:", err)
return
}
defer file.Close()
for {
fileInfos, err := file.Readdir(100)
if err != nil {
if err == io.EOF {
break
}
fmt.Println("readdir error:", err)
return
}
if len(fileInfos) == 0 {
break
}
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
if deep > 0 {
ReadAndOutputDir(filepath.Join(rootPath, string(os.PathSeparator), fileInfo.Name()), deep-1)
}
} else {
fmt.Println("file:", fileInfo.Name(), "in directory:", rootPath)
}
}
}
}