In a Go program, you need to ensure that a file is closed after all the operations on it are completed, regardless of whether an error occurred. Which feature of Go would you use to achieve this?

  • Defer statements
  • Error handling (defer)
  • File handling (defer)
  • Panic and recover
Defer statements in Go allow you to postpone the execution of a function until the surrounding function returns. This feature is commonly used to ensure resources like files are properly closed after use.
Add your answer
Loading...

Leave a comment

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