Hone logo
Hone
Problems

Robust File Processing with Error Handling in Go

This challenge focuses on implementing robust error handling in Go when processing a file. Many real-world applications involve reading and writing files, and gracefully handling potential errors like file not found, permission issues, or invalid data is crucial for application stability and user experience. You'll write a Go program that attempts to read a file, process its contents (in this case, simply counting the number of lines), and handle any errors that occur during the process.

Problem Description

You are tasked with creating a Go program that reads a text file specified by the user and counts the number of lines within it. The program should handle the following potential errors gracefully:

  • File Not Found: The specified file does not exist.
  • Permission Denied: The program does not have the necessary permissions to read the file.
  • Invalid Input: The user provides an invalid file path (e.g., empty string, contains illegal characters).
  • Other I/O Errors: Any other errors that might occur during file reading.

The program should:

  1. Prompt the user to enter the file path.
  2. Validate the file path (ensure it's not empty).
  3. Attempt to open the file.
  4. If the file opens successfully, read the file line by line and count the number of lines.
  5. If any error occurs during the process (file opening, reading, or validation), print an informative error message to the console and exit with a non-zero exit code (using os.Exit(1)).
  6. If the file is processed successfully, print the number of lines to the console and exit with a zero exit code.

Examples

Example 1:

Input: file.txt (where file.txt exists and contains 3 lines)
Output: File contains 3 lines.
Explanation: The program successfully opens and reads the file, counts the lines, and prints the result.

Example 2:

Input: non_existent_file.txt
Output: Error: File not found.
Explanation: The program attempts to open the file but fails because it doesn't exist. An appropriate error message is printed.

Example 3:

Input: "" (empty string)
Output: Error: Invalid file path.
Explanation: The program validates the input and detects an empty file path, printing an error message.

Example 4:

Input: /root/sensitive_file.txt (assuming the user doesn't have read permissions)
Output: Error: Permission denied.
Explanation: The program attempts to open the file but fails due to insufficient permissions. An appropriate error message is printed.

Constraints

  • The file path provided by the user can be up to 256 characters long.
  • The program must handle all specified error conditions.
  • The program should exit with a non-zero exit code (1) upon encountering an error.
  • The program should exit with a zero exit code (0) upon successful completion.
  • The program should use standard Go libraries for file I/O and error handling.

Notes

  • Consider using the os and io/ioutil packages for file operations.
  • The os.Stat function can be helpful for checking if a file exists and determining its permissions before attempting to open it. However, it's not strictly required.
  • Use defer to ensure that file resources are properly closed, even in the presence of errors.
  • Focus on providing clear and informative error messages to the user. Avoid generic "something went wrong" messages.
  • Think about how to handle potential errors during the line counting process (although this is less likely).
Loading editor...
go