To remove a directory in Golang using the os
package, you can use the RemoveAll
function. Here's an example:
package main
import (
"fmt"
"os"
)
func main() {
dirPath := "/path/to/directory"
err := os.RemoveAll(dirPath)
if err != nil {
fmt.Println("Failed to remove directory:", err)
return
}
fmt.Println("Directory removed successfully")
}
In this example, RemoveAll
function is used to remove the directory at the specified dirPath
. If an error occurs, it prints the error message. Otherwise, it prints a success message.