This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

preface

Every programming language has its own system library, and Golang is no exception. Today we’ll discuss Golang’s OS package’s two removal methods: Remove and RemoveAll. Both of them can delete files, but there are certain differences, understand these differences, in the daily coding will not appear “double rabbit along the ground, Ann can distinguish me is male and female” dilemma.

The body of the

If you know the Golang delete operation, you can use os.remove () or os.removeall () when you want to delete a file. If you can delete files, what’s the difference between them? Today we’ll take a look at some of those differences.

Delete the file

os.Remove()

Next, we use the os.remove () method to Remove a file. Since we need to verify with code, we first need to create a file called test.txt and then delete it. Here’s the code:

package main

import ( 
    "os"
    "fmt"
)
 
func main (a) {
 
	testFile := "test.txt"
	_, err := os.Create(testFile) // Create the file
	iferr ! =nil {
		fmt.Println("File creation failed")}// Use os.remove () to Remove the file
        err = os.Remove(testFile)
 
	iferr ! =nil {
		fmt.Println("Deletion failed")}else {
		fmt.Println("Deletion successful")}}Copy the code

Code execution result:

Delete the success

os.RemoveAll()

Ok, from the above example, we can see that the os.remove () method is quite convenient to Remove files. So, let’s take a look at how os.removeall () performs. Again, let’s replace the delete method. The modified code is as follows:

package main

import (
	"os"
	"fmt"
)
 
func main (a) {
 
	testFile := "test.txt"
	_, err := os.Create(testFile) // Create the file
	iferr ! =nil {
		fmt.Println("File creation failed")}// Use os.removeall () to remove the file
	err = os.RemoveAll(testFile)
 
	iferr ! =nil {
		fmt.Println("Deletion failed")}else {
		fmt.Println("Deletion successful")}}Copy the code

Code execution result:

Delete the success

Delete the directory

os.Remove()

What happens if you use os.remove () to Remove a directory? Let’s go straight to the code!

Example code:

package main

import (
	"os"
	"fmt"
)
 
func main (a) {
 
	testDir := "d1/d2/d3"
	// Create multi-level directories
	err := os.MkdirAll(testDir, os.ModePerm)
	iferr ! =nil {
		fmt.Println("File creation failed", err)
	}
        // Use os.remove () to Remove the file
	err = os.Remove(testDir)
 
	iferr ! =nil {
		fmt.Println("Deletion failed", err)
	} else {
		fmt.Println("Deletion successful")}}Copy the code

Code execution result:

Delete the success

os.RemoveAll()

So, what does the os.removeall () method do to remove a directory? Also directly look at the code!

Example code:

package main

import (
	"os"
	"fmt"
)
 
func main (a) {
 
	testDir := "d1/d2/d3"
	// Create multi-level directories
	err := os.MkdirAll(testDir, os.ModePerm)
	iferr ! =nil {
		fmt.Println("File creation failed", err)
	}
        // Use os.removeall () to remove the file
	err = os.RemoveAll(testDir)
 
	iferr ! =nil {
		fmt.Println("Deletion failed")}else {
		fmt.Println("Deletion successful")}}Copy the code

Code execution result:

Delete the success

Now, when deleting a directory, both methods have the same effect. Is there no difference between the two methods? The answer is no. If we delete d2 instead of d3, what will happen?

Os.removeall () does not work at all, but os.remove () does not.

remove d1/d2/: directory not empty

Yes, directory D2 is not empty, because there is also subdirectory d3, so we finally find out what the difference between os.removeall () and os.remove () is.

conclusion

There is not much difference between the os.removeall () and os.remove () methods when deleting files. However, when deleting a directory, os.remove () can only Remove an empty directory, while os.removeall () can be deleted without any restrictions.

Calendar Punch in (August Challenge)