What does the 'defer' keyword do in Go?
- Delays execution of a function until the end of the surrounding function
- Executes a function after a specified time interval
- Executes a function immediately
- Skips execution of a function
In Go, the defer keyword delays the execution of a function until the surrounding function returns. This is commonly used to ensure that cleanup tasks or finalizing actions are performed regardless of how the function exits, such as closing files or releasing resources.
Loading...
Related Quiz
- You're developing a package in Go that interacts with multiple external libraries. Which feature would you use to handle different types returned by these libraries in a flexible way?
- Middleware in Go often modifies the _______ of incoming HTTP requests before they reach the main request handler.
- Which method in the database/sql package is used to close a database connection?
- The method Marshal in Go is used to _____ a struct into JSON.
- In Go, what is the purpose of a package?