Is `closeErr` going to be set as expected if `conn.Close()` returns an error? If so, is this pattern already known/used? If not, can I claim it and become rich and famous?
Antoine Grondin
unread,
Dec 23, 2013, 1:16:42 PM12/23/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
I failed at making a proper snippet, forgetting to cleanup my original code to make a nice example. Here's a proper one if the previous link disturbed you:
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com
No, because the return effectively evaluates the return value and stores it before any defers execute. But using named parameters will work because they provide access to the actual return values:
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Matthew Kane, golang-nuts
To expand, consider if `err` was set by another operation before the return call. Say, a `conn.Read` failed. Then returning, I would not want a following `Close` fail to hide the original error. So I came up with this:
However it's not very elegant. Any thought on how to do this better?
Cheers,
--
Antoine Grondin
Live long and prosper
Antoine Grondin
unread,
Dec 23, 2013, 3:29:13 PM12/23/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Matthew Kane, golang-nuts
dsal on IRC suggested http://play.golang.org/p/RjWJlPE5jY, which is awkwardly something I used before. I guess that solves my last question.
Cheers,
--
Antoine Grondin
Live long and prosper
Kevin Malachowski
unread,
Dec 23, 2013, 6:42:56 PM12/23/13
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to golan...@googlegroups.com, Matthew Kane
Note that if you do this (or this sort of thing) often you can always structure the error checking code into another function similar to this: http://play.golang.org/p/EKQZfiw0_K
Even if you keep it the error checking as a closure like you had it, you can still simplify some of your code if you invert your conditional and use the simple statement-condition "if" that go has (like the CloseWithErrors function in my example)