Skip to content

Archive

Defer

1 articles
Go 16 Sep 2026 4 min read

Go Defer Saves Call Arguments Before Return

A Go defer statement evaluates its function value and call parameters when execution reaches the statement, even though the deferred function runs only as the surrounding function returns. Mutations between those two moments do not retroactively change already saved argument values. This split between evaluation and invocation is part of the language semantics rather than an optimization detail. Each executed defer records a call with values established at that point. Return processing later invokes recorded calls in reverse registration order.