Skip to content

Commit

Permalink
#71: Fix deferred error checks (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman authored Apr 8, 2024
1 parent 4557d4e commit de662e4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 6 additions & 1 deletion files.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,12 @@ func FilePutContents(fileName, data string, flags ...interface{}) (int, error) {
}

f, err := os.OpenFile(fileName, v|os.O_WRONLY, 0644)
defer f.Close()
defer func(f *os.File) {
err = f.Close()
if err != nil {
fmt.Println(err)
}
}(f)

if err != nil {
return -1, err
Expand Down
23 changes: 20 additions & 3 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
)
Expand All @@ -27,7 +28,12 @@ func (c *Context) doRequest(path string) (string, error) {
return "", err
}

defer resp.Body.Close()
defer func(body io.ReadCloser) {
err = body.Close()
if err != nil {
fmt.Println(err)
}
}(resp.Body)

content, cErr := io.ReadAll(resp.Body)

Expand Down Expand Up @@ -56,15 +62,26 @@ func (c *Context) uploadFile(fieldName, filePath string) bool {
return false
}

defer file.Close()
defer func(file multipart.File) {
err = file.Close()
if err != nil {
fmt.Println(err)
}
}(file)

f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return false
}

defer f.Close()
defer func(f *os.File) {
err = f.Close()
if err != nil {
fmt.Println(err)
}
}(f)

_, err = io.Copy(f, file)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit de662e4

Please sign in to comment.