-
Notifications
You must be signed in to change notification settings - Fork 75
Close file handle after use in generated code #27
Conversation
|
👍 |
|
took the liberty of fixing travis too |
main.go
Outdated
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer f.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for this to be a defer. Save the results of ReadAll, close, then return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't it the same but w/o having to store the result of ReadAll and check for the error? meh though it's less code like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also you need to make sure to Close on ReadAll failure as well no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b, err := ioutil.ReadAll(f)
if err != nil {
f.Close()
return nil, err
}
f.Close()
return b, nil
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defer is a slow operation, and so shouldn't be used if there's only one return path. There's no difference in the return values if ReadAll returns an error, so the extra logic you have above doesn't add any value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand, if readall fails you still have to close the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b, err := ioutil.ReadAll(f)
f.Close()
return b, err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok i see nvm
|
@mjibson updated |
h/t @runcom