A Journey With Go
Published in

A Journey With Go

Go: How Does defer Statement Work?

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ 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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store