How to remove all contents of a directory using Golang?

前端 未结 4 399
灰色年华
灰色年华 2021-01-31 06:58

I\'m new to Go and can\'t seem to find a way to delete all the contents of a directory when I don\'t know the contents.

I\'ve tried:

os.RemoveAll(\"/tmp/         


        
相关标签:
4条回答
  • 2021-01-31 07:29
    func RemoveContents(dir string) error {
        files, err := filepath.Glob(filepath.Join(dir, "*"))
        if err != nil {
            return err
        }
        for _, file := range files {
            err = os.RemoveAll(file)
            if err != nil {
                return err
            }
        }
        return nil
    }
    
    0 讨论(0)
  • 2021-01-31 07:33

    Write a simple RemoveContents function. For example,

    package main
    
    import (
        "fmt"
        "os"
        "path/filepath"
        "strings"
    )
    
    func RemoveContents(dir string) error {
        d, err := os.Open(dir)
        if err != nil {
            return err
        }
        defer d.Close()
        names, err := d.Readdirnames(-1)
        if err != nil {
            return err
        }
        for _, name := range names {
            err = os.RemoveAll(filepath.Join(dir, name))
            if err != nil {
                return err
            }
        }
        return nil
    }
    
    func main() {
        dir := strings.TrimSuffix(filepath.Base(os.Args[0]), filepath.Ext(os.Args[0]))
        dir = filepath.Join(os.TempDir(), dir)
        dirs := filepath.Join(dir, `tmpdir`)
        err := os.MkdirAll(dirs, 0777)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        file := filepath.Join(dir, `tmpfile`)
        f, err := os.Create(file)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        f.Close()
        file = filepath.Join(dirs, `tmpfile`)
        f, err = os.Create(file)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
        f.Close()
    
        err = RemoveContents(dir)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
    
    0 讨论(0)
  • 2021-01-31 07:48

    It might be a stupid answer, but why not simply do?

    os.RemoveAll("/tmp/")
    os.MkdirAll("/tmp/",FileMode)
    
    0 讨论(0)
  • 2021-01-31 07:48

    Just use ioutil.ReadDir to get a slice of os.FileInfo types, then iterate through and remove each child item using os.RemoveAll.

    package main
    
    import (
        "io/ioutil"
        "os"
        "path"
    )
    
    func main() {
        dir, err := ioutil.ReadDir("/tmp")
        for _, d := range dir {
            os.RemoveAll(path.Join([]string{"tmp", d.Name()}...))
        }
    }
    

    That way, you are removing only all the child items and not the parent /tmp folder itself.

    I have used this pattern many times before (e.g. test logs, etc.).

    0 讨论(0)
提交回复
热议问题