
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 a Directory
In this golang article, we will delete a directory in Golang using thhe os.Remove() function as well as using os.RemoveAll() function. There are many inbuilt functions present in go to remove a directory, and we will discuss two of such methods in this program.
In computer languages a directory is a container or a file system object that contains information about files and other directories.
Syntax
funcRemove(file_name string) error
The remove function is present in os package and is used to remove a particular file or directory. The function accepts one argument which is the name of the file to be removed and returns the error variable which contains a value if there is a problem in removing the required file.
funcRemoveAll(path string) error
The RemoveAll() function is present in os package and is used to remove the given file or directory. The function accepts one argument which is the path length of the directory to be removed. The function returns an error variable as a result.
Algorithm
First, we need to import the fmt and os packages.
Then, start the main() function. Inside the main() call the respective function and pass in the path of the directory to be deleted as an argument.
Check if an error occurred during the deletion process by checking the value of the returned error.
If there was no error, print a message to the console indicating that the directory was deleted successfully.
If there was an error, print the error message to the console.
Example 1
In this Example, we first import the fmt and os packages. The os.Remove() function is called with the path of the directory to be deleted. If the deletion is successful, a message is printed to the console. If an error occurs, the error message is printed.
package main import ( "fmt" "os" ) func main() { // enter path of the directory to be removed err := os.Remove("new") if err != nil { fmt.Println(err) } else { fmt.Println("Directory deleted successfully") } }
Output
Directory deleted successfully
Example 2
In this Example, we use the os.RemoveAll() function in the same way as we used the os.Remove() function in the previous Example. The difference is that os.RemoveAll() deletes not only the directory but also all of its contents, including subdirectories and files.
package main import ( "fmt" "os" ) func main() { err := os.RemoveAll("new") if err != nil { fmt.Println(err) } else { fmt.Println("Directory and its contents deleted successfully") } }
Output
Directory and its contents deleted successfully
Conclusion
We have successfully compiled and executed a go language program to delete a directory along with Examples. Deleting a directory in Golang can be done by using the os.Remove(), os.RemoveAll(). The os.Remove() function deletes a single directory, while the os.RemoveAll() function deletes a directory and all its contents. The method you choose will depend on your specific use case and requirements. Regardless of the method chosen, it is important to handle any errors that may occur during the deletion process.