Reputation: 22254
In this example, a deferred function increments the return value I after the surrounding function returns. Thus, this function returns 2:
func c() (i int) {
defer func() { i++ }()
return 1
}
How come this returns 2?
The go blog Defer, Panic, and Recover
Deferred functions may read and assign to the returning function's named return values.
I suppose the programmer's intention is to return 1 as the result, and wonder why it is overriding, and what design decision is behind.
Upvotes: 1
Views: 1109
Reputation: 1419
Deferred anonymous functions may access and modify the surrounding function’s named return parameters.
From the spec:
For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned. If the deferred function has any return values, they are discarded when the function completes.
Upvotes: 0
Reputation: 16243
Deferred functions are executed after any result parameters are set by that return statement but before the function returns to its caller.
Order of execution:
func c() (i int) { // i = 0
defer func() { i++ }()
return 1 // i = 1
// run deferred function here i++ => i = 2
}
See an example for defer
keyword.
See Defer_statements:
Each time a "defer" statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked. Instead, deferred functions are invoked immediately before the surrounding function returns, in the reverse order they were deferred. That is, if the surrounding function returns through an explicit return statement, deferred functions are executed after any result parameters are set by that return statement but before the function returns to its caller. If a deferred function value evaluates to nil, execution panics when the function is invoked, not when the "defer" statement is executed.
For instance, if the deferred function is a function literal and the surrounding function has named result parameters that are in scope within the literal, the deferred function may access and modify the result parameters before they are returned. If the deferred function has any return values, they are discarded when the function completes. (See also the section on handling panics.)
lock(l)
defer unlock(l) // unlocking happens before surrounding function returns
// prints 3 2 1 0 before surrounding function returns
for i := 0; i <= 3; i++ {
defer fmt.Print(i)
}
// f returns 42
func f() (result int) {
defer func() {
// result is accessed after it was set to 6 by the return statement
result *= 7
}()
return 6
}
Upvotes: 4