Skip to content
Middle+184 views

What Go language construct allows executing an action upon exiting a goroutine/function scope?

Practice

AI answer

It’s defer. It postpones a function call until the current function finishes and executes deferred calls in LIFO order. For example, defer file.Close() is useful for releasing resources. defer does not run simply when exiting an arbitrary scope.