Go Program to Delete a File

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

  1. Import the Necessary Packages: Use import "fmt" and import "os" for file operations and formatted I/O.
  2. Write a Function to Delete the File: Implement a function that deletes the file using os.Remove.
  3. Write the Main Function: Define the main function, which is the entry point of every Go program.
  4. Prompt the User to Enter a Filename: Use fmt.Scanln or fmt.Scanf to take the filename as input.
  5. Call the Function to Delete the File: Use the function to delete the specified file.
  6. Display the Result: Use fmt.Println to 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 deleteFile function uses the os.Remove function to delete the file specified by filename.
    • os.Remove attempts 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.Remove returns nil.

Step 3: Write the Main Function

  • The main function prompts the user for a filename and calls deleteFile to delete the file.

Step 4: Prompt the User to Enter a Filename

  • The program prompts the user to enter the filename using fmt.Print and reads the input using fmt.Scanln.

Step 5: Call the Function to Delete the File

  • The program calls deleteFile with 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top