How to Delete or Remove a File in Golang?
In the Go language, you are allowed to remove the existing file with the help of the Remove() method. This method removes the specified file from the director or it also removes empty directories. If the given path is incorrect, then it will throw an error of type *PathError. It is defined under the os package so, you have to import os package in your program for accessing Remove() function.
Syntax:
func Remove(file_name string) error
Example 1:
package main
import (
"log"
"os"
)
func main() {
e := os.Remove( "GeeksforGeeks.txt" )
if e != nil {
log .Fatal(e)
}
}
|
Output:
Before:
After:
Example 2:
package main
import (
"log"
"os"
)
func main() {
e := os.Remove( "/Users/anki/Documents/new_folder/GeeksforGeeks.txt" )
if e != nil {
log .Fatal(e)
}
}
|
Output:
Before:
After:
Last Updated :
02 Apr, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...