How to remove all contents of a directory using Golang?

前端 未结 4 400
灰色年华
灰色年华 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
    }
    

提交回复
热议问题