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.
Add your answer
Loading...

Leave a comment

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