Practice 50 Go Lang Error Handling and Logging Coding Questions, TechnoVlogs

Practice 50 Go Lang Error Handling and Logging Coding Questions


Q1. Write a Go program that handles errors in a file opening operation and logs the error if the file does not exist.  
  Input:    
  Try opening the file "nonexistentfile.txt"
  Expected Output:  
  Error: open nonexistentfile.txt: no such file or directory

Q2. Write a Go program that handles division by zero errors and logs an appropriate message.  
  Input:    
  Divide 10 by 0
  Expected Output:  
  Error: division by zero

Q3. Write a Go program that checks if a file exists and logs an error if the file is missing.  
  Input:    
  Check if "config.txt" exists
  Expected Output:  
  Error: config.txt not found

Q4. Write a Go program to handle a situation where an invalid type is provided for an expected integer and log the error.  
  Input:    
  Parse "abc" as an integer
  Expected Output:  
  Error: strconv.Atoi: parsing "abc": invalid syntax

Q5. Write a Go program that reads a file and logs an error if the file is empty.  
  Input:    
  Read the file "emptyfile.txt"
  Expected Output:  
  Error: file is empty

Q6. Write a Go program that logs an error if a network connection cannot be established.  
  Input:    
  Attempt to connect to "http://invalid-url"
  Expected Output:  
  Error: cannot establish connection to http://invalid-url

Q7. Write a Go program that attempts to parse a date string and logs an error if the format is incorrect.  
  Input:    
  Parse the date string "2025-13-32"
  Expected Output:  
  Error: invalid date format

Q8. Write a Go program that logs an error when trying to access an index outside the range of an array.  
  Input:    
  Access array[10] where array = [1, 2, 3]
  Expected Output:  
  Error: index out of range

Q9. Write a Go program that attempts to convert a string into a float and logs an error if the string is not valid.  
  Input:    
  Convert "abc" to float
  Expected Output:  
  Error: strconv.ParseFloat: parsing "abc": invalid syntax

Q10. Write a Go program to handle and log errors when trying to connect to a database with wrong credentials.  
   Input:     
   Attempt database connection with invalid credentials
   Expected Output:  
   Error: unable to connect to database: invalid credentials

Q11. Write a Go program that logs an error when it cannot write to a file due to permission issues.  
   Input:     
   Attempt to write to "/protected/file.txt"
   Expected Output:  
   Error: permission denied

Q12. Write a Go program that catches and logs a panic when an invalid array index is accessed.  
   Input:     
   Access array[10] where array = [1, 2, 3]
   Expected Output:  
   Panic: runtime error: index out of range

Q13. Write a Go program that logs an error when it encounters an unsupported file format during file upload.  
   Input:     
   Attempt to upload a file with extension ".exe"
   Expected Output:  
   Error: unsupported file format

Q14. Write a Go program to handle an HTTP request that fails due to a missing query parameter and logs the error.  
   Input:     
   Send a GET request to "/search" without the query parameter "term"
   Expected Output:  
   Error: missing required query parameter "term"

Q15. Write a Go program that attempts to read from a closed channel and logs an error.  
   Input:     
   Read from a closed channel
   Expected Output:  
   Error: cannot read from closed channel

Q16. Write a Go program that logs an error when a user tries to access a route without proper authentication.  
   Input:     
   Access "/dashboard" without a valid session
   Expected Output:  
   Error: unauthorized access

Q17. Write a Go program that logs an error if the server cannot bind to the specified port.  
   Input:     
   Start server on port 8080 when port is already in use
   Expected Output:  
   Error: cannot bind to port 8080

Q18. Write a Go program that logs an error when trying to write an invalid JSON object to a file.  
   Input:     
   Write {name: "John", age: } to a file
   Expected Output:  
   Error: invalid JSON format

Q19. Write a Go program that catches and logs an error when trying to open an unsupported file type.  
   Input:     
   Open "file.xyz"
   Expected Output:  
   Error: unsupported file type

Q20. Write a Go program that logs an error when an expected environment variable is missing.  
   Input:     
   Read environment variable "API_KEY"
   Expected Output:  
   Error: API_KEY not set

Q21. Write a Go program that logs an error if the server fails to respond within the timeout period.  
   Input:     
   Send an HTTP request with a timeout of 1 second
   Expected Output:  
   Error: server response timed out

Q22. Write a Go program that logs an error when the input is not a valid email address.  
   Input:     
   Validate "invalid-email"
   Expected Output:  
   Error: invalid email address format

Q23. Write a Go program that logs an error when trying to open a non-existent URL using http.Get.  
   Input:     
   Call http.Get("http://nonexistent-url.com")
   Expected Output:  
   Error: failed to fetch http://nonexistent-url.com

Q24. Write a Go program that logs an error if a database record is not found.  
   Input:     
   Search for user with ID 9999 in the database
   Expected Output:  
   Error: user with ID 9999 not found

Q25. Write a Go program that logs an error when an API request returns a non-200 status code.  
   Input:     
   Send a GET request to an API that returns 404
   Expected Output:  
   Error: received non-200 status code 404

Q26. Write a Go program that logs an error when trying to parse invalid JSON data.  
   Input:     
   Parse "{name: John}" (invalid JSON)
   Expected Output:  
   Error: invalid character 'n' in literal true (expecting 'r')

Q27. Write a Go program that logs an error when file permissions prevent reading from a file.  
   Input:     
   Try reading from "readonlyfile.txt"
   Expected Output:  
   Error: permission denied

Q28. Write a Go program that logs an error when trying to connect to an unsupported protocol.  
   Input:     
   Attempt to connect to ftp://example.com
   Expected Output:  
   Error: unsupported protocol "ftp"

Q29. Write a Go program that handles and logs an error when attempting to decode an empty JSON object.  
   Input:     
   Decode "{}" into a Go struct
   Expected Output:  
   Error: failed to decode empty JSON object

Q30. Write a Go program that catches and logs an error when a panic occurs due to accessing an invalid memory address.  
   Input:     
   Dereference a nil pointer
   Expected Output:  
   Panic: runtime error: invalid memory address or nil pointer dereference

Q31. Write a Go program that logs an error when trying to parse an invalid URL.  
   Input:     
   Parse "htp://invalid-url"
   Expected Output:  
   Error: invalid URL format

Q32. Write a Go program that logs an error when attempting to open a file that the program does not have permission to access.  
   Input:  
   Open "/etc/sensitivefile.txt"
   Expected Output:  
   Error: permission denied to access /etc/sensitivefile.txt

Q33. Write a Go program that handles and logs an error when an API call fails due to timeout.  
   Input:     
   Call a slow API with a timeout of 2 seconds
   Expected Output:  
   Error: API call timed out

Q34. Write a Go program that handles a missing file path error and logs it.  
   Input:     
   Try opening "path/to/nonexistentfile.txt"
   Expected Output:  
   Error: file not found at path/to/nonexistentfile.txt

Q35. Write a Go program that handles an error when trying to connect to a MySQL database with incorrect credentials.  
   Input:     
   Try connecting to a MySQL database with invalid username/password
   Expected Output: 
   Error: MySQL authentication failed: incorrect username or password

Q36. Write a Go program that logs an error when a specific API endpoint is not found.  
   Input:     
   Access "http://localhost:8080/invalidendpoint"
   Expected Output:  
   Error: 404 Not Found for /invalidendpoint

Q37. Write a Go program that logs an error when the server cannot write to the response due to a network issue.  
   Input:     
   Send data to the client but the connection is closed unexpectedly
   Expected Output:  
   Error: failed to write response due to network issue

Q38. Write a Go program that logs an error when a user tries to delete a record that does not exist in the database.  
   Input:     
   Attempt to delete user with ID 9999
   Expected Output:  
   Error: record with ID 9999 not found for deletion

Q39. Write a Go program that logs an error when an unsupported HTTP method is used.  
   Input:     
   Send a PATCH request to http://localhost:8080/resource
   Expected Output:  
   Error: unsupported HTTP method PATCH

Q40. Write a Go program that logs an error when an invalid response is returned from a web service.  
   Input:     
   Call a web service and receive an unexpected status code 500
   Expected Output:  
   Error: received unexpected status code 500 from web service

Q41. Write a Go program that logs an error when an invalid file extension is uploaded by the user.  
   Input:     
   Upload file "image.exe"
   Expected Output:  
   Error: invalid file type uploaded

Q42. Write a Go program that logs an error when a JSON response contains unexpected data types.  
   Input:     
   Receive JSON response: {"age": "twenty"}
   Expected Output:  
   Error: invalid type for "age" field, expected integer

Q43. Write a Go program that logs an error when trying to read an unsupported image file format.  
   Input:     
   Read image file "photo.xyz"
   Expected Output:  
   Error: unsupported image file format

Q44. Write a Go program that logs an error when a required field is missing in the input data.  
   Input:     
   POST request with data: {"name": "Alice"}
   Expected Output:  
   Error: missing required field "email"

Q45. Write a Go program that logs an error when an invalid API token is used in the request.  
   Input:     
   Make API request with invalid token
   Expected Output:  
   Error: invalid API token

Q46. Write a Go program that logs an error when it fails to load a configuration file.  
   Input:     
   Try loading the "config.yaml" file
   Expected Output:  
   Error: failed to load configuration file

Q47. Write a Go program that logs an error when a system resource limit is exceeded.  
   Input:     
   Attempt to allocate too much memory
   Expected Output:  
   Error: resource limit exceeded

Q48. Write a Go program that logs an error when the request body exceeds the maximum allowed size.  
   Input:     
   Send a large payload exceeding 10MB
   Expected Output:  
   Error: request body exceeds the maximum allowed size

Q49. Write a Go program that logs an error when an invalid HTTP status code is received in response to an API call.  
   Input:     
   API call returns status code 400
   Expected Output:  
   Error: unexpected status code 400 received

Q50. Write a Go program that logs an error when the file path is not absolute.  
   Input:     
   Attempt to open the file "myfile.txt" from a relative path
   Expected Output:  
   Error: invalid file path, expected absolute path

Share on Social Media