On Windows, you cannot rename a currently opened file.
Remove the defer Close() and explicitly call it again.