- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Golang program to delete empty and non-empty directory
In golang, we can use os and dir packages to delete a given directory. Here we are using two examples to demonstrate to deletion of an empty and non-empty directory using os.open, os.Removeall.
Syntax
os.Open
This function is a part of os package. It is used to open a file to read. It takes one input i.e. the filename which will be opened.
os.RemoveAll
It removes the directory and its contents completely. The function takes the name of the directory as an argument.
dr.ReaddirNames
This function is useful to read the file names from a directory. It takes the directory name as the argument.
Algorithm
Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.
Step 2 − Use os.Open function to access the directory and save its reference in the dr variable.
Step 3 − In the next Step, using dr.Readdirnames read the names of each file and subdirectory in the directory.
Step 4 − Use os.RemoveAll to delete each file and subdirectory after iterating over their names in a loop.
Step 5 − Use os.Remove to remove the directory itself.
Example 1
In this example, we will use os package functions to delete the empty and non-empty directory.
package main import ( "fmt" "os" ) func delete_directory(path string) error { dr, err := os.Open(path) //open the path using os package function if err != nil { return err } defer dr.Close() names, err := dr.Readdirnames(-1) if err != nil { return err } for _, name := range names { err = os.RemoveAll(path + "/" + name) //remove the path if err != nil { return err } } return os.Remove(path) } func main() { path := "/path/to/directory" err := delete_directory(path) if err != nil { fmt.Println("Error deleting directory:", err) os.Exit(1) } fmt.Println("Directory is deleted successfully") //if directory is deleted print the success message }
Output
Directory is deleted successfully
Example 2
In this example, errorf function will be used to print error if any error occurs while deleting the directory.
package main import ( "fmt" "os" ) func delete_directory(path string) error { errs := os.RemoveAll(path) //use the os package function to delete dir if errs != nil { return fmt.Errorf("Error deleting directory: %v", errs) } return nil } func main() { path := "/path/to/directory" errs := delete_directory(path) if errs != nil { fmt.Println(errs) //print the error if directory is not deleted successfully os.Exit(1) } fmt.Println("Directory is deleted successfully") //print the success message if directory is deleted successfully }
Output
Directory is deleted successfully
Conclusion
We executed the program of deleting empty and non-empty directory using two examples. In the first example we used os package functions and Readdirnames whereas in the second example we used errorf function to execute the program.