Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay execution of closing a file

Tags:

go

I remember reading about a feature of go in that you can declare a file handle to be closed when you first open it, and at the end of the (function? method? garbage collection?) it would automatically close.

What is the syntax for this and what is it called?

like image 279
Ryan Leach Avatar asked Sep 17 '13 08:09

Ryan Leach


1 Answers

It sounds like you are after the defer keyword. This lets you specify statements that will be executed when the function exits. For example:

defer f.Close()

Deferred cleanup routines are executed in the opposite order they are created.

like image 99
James Henstridge Avatar answered Oct 16 '22 23:10

James Henstridge