New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: Go 2: add check err goto handler #37035
Comments
This seems a bit like "ON ERROR GOTO" in (Visual) Basic. (https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/on-error-statement). If we wanted this in Go, we could perhaps do something like this to stay backwards compatible: func Foo() {
var err error
goto handler for err // will execute goto handler whenever err becomes non-nil, only error type is allowed.
err = mayFail1()
err = mayFail2()
handler: {
// do something with err here
}
} While "ON ERROR GOTO" is a simple and familiar feature of programming languages, I am undecided whether it is a good feature or not. It overlaps with defer() in some possibly confusing ways. |
This looks like it's ambiguous? |
It sounds like you are suggesting that we replace if err != nil {
goto handler
} with check err goto handler That introduces a new keyword It also makes We also have to consider code like f, err := os.Open("x")
check err goto handler
s := "str"
handler:
return s We don't permit a normal 'goto' to jump over a variable declaration like that, and we probably don't want |
For language change proposals, please fill out the template at https://go.googlesource.com/proposal/+/bd3ac287ccbebb2d12a386f1f1447876dd74b54d/go2-language-changes.md . When you are done, please reply to the issue with Thanks! |
Thank you for your reply, which made me realize that |
@gopherbot please remove label WaitingForInfo |
Are you withdrawing the proposal? |
I had an Epiphany last night like this:
Only the second value is detected each time, and if more than one value is returned, it is recommended to encapsulate it as a type,
If you provide a keyword to detect whether the second variable is nil like this:
This makes it compatible with previous or future error handling,Can you solve the problem of error handling
Finally , I'm sorry for my poor English
The text was updated successfully, but these errors were encountered: