Introduction
Deleting a file is a basic file operation that can be easily performed in Go using the os package. This guide will demonstrate how to write a Go program that deletes a specified file.
Problem Statement
Create a Go program that:
- Takes a filename as input.
- Deletes the file if it exists.
- Displays a message indicating whether the file was successfully deleted or if an error occurred.
Example:
- Input:
"example.txt" - Output:
File deleted successfully.or
Error deleting file: file does not exist.
Solution Steps
- Import the Necessary Packages: Use
import "fmt"andimport "os"for file operations and formatted I/O. - Write a Function to Delete the File: Implement a function that deletes the file using
os.Remove. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Prompt the User to Enter a Filename: Use
fmt.Scanlnorfmt.Scanfto take the filename as input. - Call the Function to Delete the File: Use the function to delete the specified file.
- Display the Result: Use
fmt.Printlnto display whether the file was successfully deleted or if an error occurred.
Go Program
package main
import (
"fmt"
"os"
)
// Step 2: Implement a function to delete the file
func deleteFile(filename string) error {
// Use os.Remove to delete the file
err := os.Remove(filename)
if err != nil {
return err
}
return nil
}
/**
* Go Program to Delete a File
* Author: https://www.javaguides.net/
*/
func main() {
// Step 4: Prompt the user to enter a filename
var filename string
fmt.Print("Enter the filename to delete: ")
fmt.Scanln(&filename)
// Step 5: Call the function to delete the file
err := deleteFile(filename)
if err != nil {
fmt.Println("Error deleting file:", err)
} else {
fmt.Println("File deleted successfully.")
}
}
Explanation
Step 2: Implement a Function to Delete the File
- The
deleteFilefunction uses theos.Removefunction to delete the file specified byfilename.os.Removeattempts to delete the file. If the file does not exist or cannot be deleted for some reason, it returns an error.- If the file is successfully deleted,
os.Removereturnsnil.
Step 3: Write the Main Function
- The
mainfunction prompts the user for a filename and callsdeleteFileto delete the file.
Step 4: Prompt the User to Enter a Filename
- The program prompts the user to enter the filename using
fmt.Printand reads the input usingfmt.Scanln.
Step 5: Call the Function to Delete the File
- The program calls
deleteFilewith the provided filename to delete the file.
Step 6: Display the Result
- The program prints a message indicating whether the file was successfully deleted or if an error occurred using
fmt.Println.
Output Example
Example 1:
Enter the filename to delete: example.txt
File deleted successfully.
Example 2:
Enter the filename to delete: nonexistentfile.txt
Error deleting file: remove nonexistentfile.txt: no such file or directory
Example 3 (Empty input):
Enter the filename to delete:
Error deleting file: remove : no such file or directory
Conclusion
This Go program demonstrates how to delete a file using the os package. It covers basic file operations such as file deletion and error handling in Go. This example is useful for beginners learning Go programming and understanding how to perform file manipulation tasks.