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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of close

close

Photo of writable files

writable files

Related news:

News photo

TechCrunch Space: The Starliner saga comes to a close — for now

News photo

Starfish Space and D-Orbit complete orbital rendezvous, bringing Otter Pup mission to a close

News photo

Google Takes Questions Over Ads as US Trial Comes to a Close