Get the latest tech news
Don't defer Close() on writable files (2017)
It'll bite you some day
It’s an idiom that quickly becomes rote to Go programmers: whenever you conjure up a value that implements the io.Closer interface, after checking for errors you immediately defer its Close() method. It means that we’ve lost data trying to save it to disk, and our Go programs should absolutely not return a nil error in that case. In longer functions with more if err != nil conditional branches, this pattern can also result in fewer lines of code and less repetition.
Or read this on Hacker News