Skip to content

Example adds a defer after the error is already returned? #6

Answered by lainio
gregwebs asked this question in Q&A
Discussion options

You must be logged in to vote

Great question! Sorry for my late reply. I was traveling this week.

Short answer: The err2.Handle with os.Remove is after the os.Create because it's needed only if io.Copy fails. I suppose the cleaning overall exists because the original authors of CopyFile wanted to show how error handling can have parts where resource cleanup is necessary. FYI, all the err2 handlers are called if they exist in the current defer stack at the moment when err != nil. Those handlers are: err2.Annotex, err2.Returnx and err2.Handle.

To be sure, I wanted to test the CopyFile in the current err2 Go documentation. These are the results, and it seems to work as meant to:

  • valid source and destination (target) fil…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by gregwebs
Comment options

You must be logged in to vote
1 reply
@lainio
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants