Hone logo
Hone
Problems

Mastering Go's Defer: Resource Management and Cleanup

The defer statement in Go is a powerful tool for ensuring that certain operations, often resource cleanup, are executed just before a function returns. This challenge will help you understand and utilize defer to manage resources reliably, preventing leaks and simplifying your code.

Problem Description

You are tasked with writing a Go function that simulates performing an operation that requires opening and closing a resource. This resource could represent a file, a network connection, or any other entity that needs explicit cleanup. Your goal is to use the defer statement to guarantee that the resource is closed, regardless of how the function exits (e.g., normal return, panic).

Requirements:

  1. Create a function, let's call it performOperation, that takes no arguments and returns a string message indicating success or failure.
  2. Inside performOperation, simulate "opening" a resource. For this simulation, you can simply print a message like "Resource opened."
  3. Immediately after "opening" the resource, use a defer statement to schedule a "closing" operation. The deferred function should print a message like "Resource closed."
  4. The performOperation function should then simulate some work. For simplicity, you can just print "Performing operation..."
  5. The function should then return a success message: "Operation completed successfully."
  6. Crucially, ensure that the "Resource closed." message is printed before the function returns, even if an error were to occur (though for this basic challenge, we won't introduce explicit errors that cause early returns).

Expected Behavior:

When performOperation() is called, the output should demonstrate the order of execution: resource opening, then operation, and finally resource closing, followed by the function's return message.

Examples

Example 1:

Input: (No input arguments for performOperation)
Output:
Resource opened.
Performing operation...
Resource closed.
Operation completed successfully.

Explanation:

The performOperation function first prints "Resource opened." Then, it schedules the "Resource closed." message to be executed later via defer. It proceeds to print "Performing operation...". Finally, just before returning, the deferred "Resource closed." message is executed. The function then returns "Operation completed successfully."

Constraints

  • The solution must be written in Go.
  • The defer statement must be used for resource closing.
  • The function should be named performOperation.
  • The function should not accept any arguments.
  • The function should return a string.

Notes

Think about the order of execution. When a defer statement is encountered, the function call within it is registered to be run later, not executed immediately. The deferred functions are executed in Last-In, First-Out (LIFO) order when the surrounding function returns. For this challenge, there's only one deferred function, so the order isn't a complex factor, but it's a fundamental aspect of defer to keep in mind for more advanced scenarios.

Loading editor...
go