Go: How Does defer Statement Work?

ℹ️ This article is based on Go 1.12.
defer
statement is a convenient way to execute a piece of code before a function returns, as explained in Golang specification:
Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred.