Using defer and go routines in same func

98 views
Skip to first unread message

Boopathi Rajaa

unread,
Apr 14, 2015, 5:06:03 AM4/14/15
to golan...@googlegroups.com
When I use go routines and wait on some channel, deferred function call gets called immediately, while the same(one called in defer) at the end of the function body gets called, well, at the end.

http://play.golang.org/p/iBhV80PB76

What am I assuming wrong here ?

Jan Mercl

unread,
Apr 14, 2015, 5:17:09 AM4/14/15
to golan...@googlegroups.com
On Tue, Apr 14, 2015 at 11:06 AM Boopathi Rajaa <legen...@gmail.com> wrote:

> What am I assuming wrong here ?

The observation that the "deferred function call gets called immediately" is not correct. The effect comes from evaluating all arguments to the deferred function when the defer statement is executed.


-j

Boopathi Rajaa

unread,
Apr 14, 2015, 5:25:41 AM4/14/15
to Jan Mercl, golan...@googlegroups.com
Awesome. Thanks.

Should have caught this one coming. 

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/eFHYCLbLHTk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages