defer schedules a statement to run when the surrounding function returns. It keeps cleanup beside the operation that requires it, so refactoring or early returns do not easily skip resource release. section 3.1.2 section 3.5
Close an opened file
f, err := os.Open(filePath) if err != nil { return nil, err } defer f.Close() The file closes when the function returns, regardless of which return path it takes. section 3.1.2


