You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With the current code example, in the case of a non-nil error, the defer isn't stacked before the return occurs, so the function will return without closing the source file.
This aspect of the example is correct and idiomatic as written.
Like most Go functions, os.Open and os.Create return zero-values (in this case, nil pointers) for the other arguments when they return a non-nil error, and calling Close on a nil receiver is generally nonsensical.
In this case, the nonsensical call is masked by the permissiveness of (*os.File).Close. Rather than panicking (as many Close implementations would for nil receivers), it returns ErrInvalid:
What did you expect to see?
What did you see instead?
With the current code example, in the case of a non-nil error, the defer isn't stacked before the return occurs, so the function will return without closing the source file.
Little program to prove my point : https://play.golang.org/p/UAdMVffys_P
The text was updated successfully, but these errors were encountered: