其他分享
首页 > 其他分享> > 98_Go基础_1_66 ioutil 遍历目录

98_Go基础_1_66 ioutil 遍历目录

作者:互联网

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "io/ioutil"
 6     "log"
 7 )
 8 
 9 func listFiles(dirname string, level int) {
10     // level用来记录当前递归的层次,生成带有层次感的空格
11     s := "|--"
12     for i := 0; i < level; i++ {
13         s = "|  " + s
14     }
15     fileInfos, err := ioutil.ReadDir(dirname)
16     if err != nil {
17         log.Fatal(err)
18     }
19     for _, fi := range fileInfos {
20         filename := dirname + "\\" + fi.Name()
21         fmt.Printf("%s%s\n", s, filename)
22         if fi.IsDir() {
23             //递归调用方法
24             listFiles(filename, level+1)
25         }
26     }
27 
28     /*
29         |--D:\Go\web2\aa.txt
30         |--D:\Go\web2\bb.txt
31         |--D:\Go\web2\demo.go
32         |--D:\Go\web2\dir1
33         |  |--D:\Go\web2\dir1\Test1927239612
34         |  |  |--D:\Go\web2\dir1\Test1927239612\text.txt1079166724
35         |  |--D:\Go\web2\dir1\a.txt
36         |  |--D:\Go\web2\dir1\b.tct
37         |--D:\Go\web2\test.png
38         |--D:\Go\web2\user.db
39     */
40 }
41 
42 func main() {
43     /**
44     遍历文件夹:
45     */
46     dirname := "D:\\Go\\web2"
47     listFiles(dirname, 0)
48 
49 }

 

标签:dir1,ioutil,level,--,web2,98,66,Go,dirname
来源: https://www.cnblogs.com/luwei0915/p/15662208.html