Flying, another way to think of it is that fn is not a function call, it is a function pointer value--a value just like 3.14 or "true." It would not make sense to defer a value, since defer is about "defer the invocation of a function." We make the function value fn become a function invocation by appending the "()" to it. This is the reason. As others have said, this is also good because it gives you a place to pass the parameters that usually are needed "defer close(file)" and because the meaning of this can be subtle (when do you record the value of file so that you can know it when close is eventually called.)
On Sun, Jan 28, 2018 at 12:39 AM, Doğan Kurt <kultigin....@gmail.com> wrote: > how will you pass parameters if its defer fn? > > > On Sunday, January 28, 2018 at 3:42:03 AM UTC+1, Flying wrote: >> >> example : >> fn := func() { >> fmt.Println(10) >> } >> defer fn() >> >> >> my question is why use defer fn() not defer fn >> >> >> when we use fn() here doesn't it mean call it now ??? >> > -- > 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. > -- Michael T. Jones michael.jo...@gmail.com -- 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.