Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close via defer #47

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) e
if err != nil {
return err
}
defer func() {
if f != nil {
spennymac marked this conversation as resolved.
Show resolved Hide resolved
_ = f.Close()
spennymac marked this conversation as resolved.
Show resolved Hide resolved
}
}()

n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}

if err1 := f.Close(); err == nil {
err1 := f.Close()
f = nil
if err == nil {
err = err1
}

Expand Down Expand Up @@ -241,7 +248,7 @@ func ReadFile(fs billy.Basic, name string) ([]byte, error) {
return nil, err
}

defer f.Close()
defer func() { _ = f.Close() }()
spennymac marked this conversation as resolved.
Show resolved Hide resolved

var size int
if info, err := fs.Stat(name); err == nil {
Expand Down