To delete a File, we need to import the 'os' module.
We can use the 'Remove()' method of 'os' module to delete a file.
package main import "fmt" import "os" func main() { err := os.Remove("myfirstfile.txt") if (err != nil) { fmt.Println("Couldn't remove file") } else { fmt.Println("Removed file successfully") } }
So, in the first line itself, we have used the, 'Remove()' method of 'os' module to delete the file 'myfirstfile.txt'.
And if the file couldn't be found, we display the message in the print statement.
if (err != nil) { fmt.Println("Couldn't remove file") }
Since, we have the file, '"myfirstfile.txt' already present. It is removed and we get the below output.