Brian Candler schrieb am Sonntag, 19. September 2021 um 20:46:45 UTC+2:

>
> I'm not sure how useful that is though, especially because "return" inside 
> that anonymous function will return from that anonymous function, not the 
> outer function.
> https://go2goplay.golang.org/p/h6ZRnqM5cRx
>

 Yes, this is a problem. Then after calling myerror.As in the next line you 
still to check whether err was not nil and if it was handled, and if so, do 
a return.

> Unfortunately, in order to use this proposed version, you still need to 
pre-declare the variables for each type

Yes, that is tedious.

To solve the problem that a return from the error block would only jump out 
of the As function, but not out of the surrounding function, either some 
special new language construct needs to be added for handling errors (any 
kind of function would always bear the problem that a return from the 
function is a local return and not a non-local as a return from an if 
clause) or things just stay with:

_, err := os.Open("non-existing")

if pe, ok := errors.As[*os.PathError](err); ok {
    fmt.Printf("Path Error: %v", pe)
} else if en, ok := errors.As[syscall.Errno](err); ok {
    fmt.Printf("errno %[1]d: %[1]s", en)
}

Not too bad, but still a bit painful considering how often you have to 
write this kind of code for all the errors that need to be handled


-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/2287acba-e01f-46d5-a905-113c479d7e95n%40googlegroups.com.

Reply via email to