Go地柜删除目录

go 创建目录

1
os.MkdirAll("./a/b/c/d/e/f",0777)

go filepath.Walk遍历目录

1
Walk(root string, walkFn WalkFunc) error

完整代码

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
package main

import (
"os"
"path/filepath"
"fmt"
)

func main() {

os.MkdirAll("./a/b/c/d/e/f",0777)
dir := "./a"
delDir(dir)
}

func delDir(dir string) {
filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
if path != dir {

if fi.IsDir() {
delDir(path)
}
fmt.Println(path)
os.Remove(path)

}

return nil
})
}