The Secret Life of Go: The 'defer' Statement
The Secret Life of Go: The 'defer' Statement # go # coding # programming # softwaredevelopment How to fix resource leaks and master Go's "Proximity Rule" Chapter 20: The Stacked Deck The fan on Ethan's desktop PC was spinning loudly. He was staring at a terminal that was spewing error messages like a broken fire hydrant. panic: too many open files panic: too many open files "I don't get it," he muttered, hitting Ctrl+C to kill the server. "I'm closing everything. I checked three times." Eleanor walked by, carrying a tray of tea. "Show me the ExportData function." Ethan pulled up the code. It was a long function, maybe 50 lines, that opened a file, queried a database, wrote to a CSV, and then uploaded it to S3. func ExportData ( id string ) error { f , err := os . Open ( "data.csv" ) if err != nil { return err } db , err := sql . Open ( "postgres" , ...