* Karan Chaudhary <fghj...@gmail.com> [170928 08:50]:
> I'm trying to find rationale for this:  why is "defer foo(x)" treated 
> differently than "defer func() { foo(x) }" by the language designers?

Here is what the language spec says:

  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.

So, in the first case, the address of foo is saved along with the
current value of x.  The variable x is not part of a closure involving
foo.

In the second case, the address of a closure, the anonymous func, is
saved; it has no function arguments to evaluate.  This closure includes
the variable x from the surrounding scope.  So when the deferred closure
is executed, the value of x at this later time is used.

Does this explain it?

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to