Tuesday, April 12, 2011

Defer, Panic, Recover: New features in GoLang



Defer can push a function call into a LIFO queue, and the function calls in this queue will execute then the surrounding function returns. Defer is perfect for cleaning procedures (like closing files after opening). Arguments are evaluated now, but deferred functions are called later.

Panic will stop the control flow of function and return immediately. Panic will trigger the execution of function calls that are previously deferred.