-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
x/tools/gopls: add some error handle postfix completion (rr, reterr, varCheckError) #64178
Comments
There is already a fancy completion that writes the entire func foo() (int, error) {
i, err := foo()
if*HERE*
} |
@muirdm Yes, It is same thing in the first case, but I think the latter two will still provide some convenience. |
@findleyr What do you think of this proposal? The pattern of error handle will not change anytime soon, so I think it's very valuable. |
Change https://go.dev/cl/550455 mentions this issue: |
@rogeryk I think this makes sense. I'll review your CL -- thanks! |
(although I'm not sure about |
In discussion on https://go.dev/cl/550455, we have settled on the more consistent snippet names |
@findleyr There is another confusing point, and I will explain it here.There is a nested tabstop in "variferr"
This is because sometimes we want to return err directly, and sometimes we want to wrap the err. So an additional tabstops is needed to modify the last returned err. Look the below picture. |
@rogeryk that's very cool, and I didn't know it worked that way. Thank you for the explanation. |
Describe the solution you'd like
Go requires that you must explicit error handling, so many times you must write some boring error handling code, and postfix completion can ease the pain.
rr
In most cases, we just need check the error and return it.
This also depends on the return parameters of the current function, for example, when the function has return values other than error, the return values other than error will use default values.
reterr
Sometimes when calling a function, you just need to check if there is an error.
varCheckError
Get the return value after calling the function and check for errors. This is a more general version compared to reterr.
The text was updated successfully, but these errors were encountered: