"err is shadowed during return": why is that bad?

8,120 views
Skip to first unread message

Lubos Pintes

unread,
Sep 10, 2013, 11:59:31 AM9/10/13
to golan...@googlegroups.com
Hi,
I have a function which returns two parameters, second of them is err error.
Then I have a for loop where I will do something like
a, b, err := SomeCall()
Then a condition "if err != nil then return".
Why err is shadowed? Because of new scope? And why compiler doesn't want
to return a value? From my point of view, this would be correct.
Thanks

Brad Fitzpatrick

unread,
Sep 10, 2013, 12:03:03 PM9/10/13
to Lubos Pintes, golang-nuts
It's a new scope, so a naked return returns the outer err, not your inner err that was != nil.

So it's almost certainly not what you meant, hence the error.





--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Lubos Pintes

unread,
Sep 10, 2013, 12:02:04 PM9/10/13
to golan...@googlegroups.com
Actually if I was explicit, "return s, err", compiler was happy.
Sorry for noise.
Reply all
Reply to author
Forward
0 new messages