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.
Loading...
Related Quiz
- How can you improve code coverage in a software project?
- Describe a scenario where managing dependencies using Go Modules improved the build process of a project.
- What function is used to read from a file in Go?
- In Go unit testing, which function prefix is used to denote a test function?
- How can panic and recover be used in error handling, and why are they generally discouraged?