When multiple 'defer' statements are used in a function, in which order are they executed?

  • In reverse order of their definition
  • In the order they are defined
  • Random order
  • Sequentially but can be interrupted
In Go, when multiple defer statements are used in a function, they are executed in reverse order of their definition. This means the defer statement that is defined last will be executed first, followed by the second-to-last, and so on. This behavior ensures that resources are properly managed and cleaned up.
Add your answer
Loading...

Leave a comment

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