Don’t defer Close() on writable files (2017)

The post discusses a common idiom in Go programming where the Close() method for writable files is deferred, potentially leading to ignored errors. It delves into the reasons behind this, including the slower nature of disk I/O compared to CPU operations. The post provides alternative solutions involving not using defer or using named return values and closures. It also mentions the safety of calling Close() multiple times on files and the importance of flushing write buffers to disk using the Sync() method. The document raises concerns about potential errors and lack of clear documentation on file behavior.

https://www.joeshaw.org/dont-defer-close-on-writable-files/

To top