-
Notifications
You must be signed in to change notification settings - Fork 18k
errors: As can be confusing to use when given a pointer to a type that does not implement error, but a pointer to that type does #37625
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
Comments
The second argument to Additionally, the pointer must be to a type that implements error, since In your original snippet, the type var _ error = ptrErr{}
// ./prog.go:15:5: cannot use ptrErr literal (type ptrErr) as type error in assignment:
// ptrErr does not implement error (Error method has pointer receiver) There isn't a way
Do you think the error text can be improved to make this more clear? |
But couldn't it check if the pointer type implements the error interface, rather than dereferencing it unconditionally first? As mentioned, its possible to workaround this by passing in a double pointer, but that is IMHO far from intuitive. |
At this time, I'm not sure if this is possible.
As far as I know, that's not just a workaround, that's the intended usage. A double pointer can be thought of as a single pointer to a value that implements the From var perr *os.PathError
if errors.As(err, &perr) {
fmt.Println(perr.Path)
} Note that Can you clarify if you're only interested in the boolean result value from |
Also see https://blog.golang.org/go1.13-errors#TOC_3.2. if you haven't already. |
I see, thanks for your responses. So I think the main source of confusion for me here was that I read the error Maybe we can reword it to sth like
Yes I am, I just tried to keep the snippet short |
I'm glad we figured out the source of confusion, that's very helpful and can be used to improve the error message. There is yet another possible interpretation: So, it should be helpful to reword the error text to make the interpretation less ambiguous. |
Change https://golang.org/cl/221877 mentions this issue: |
Make it clear that the second argument must be a non-nil pointer. The new message text matches the phrasing used in the errors.As doc. Updates golang/go#37625. Change-Id: I69dc2e34a5f3a5573030ba0f63f20e0821be1816 Reviewed-on: https://go-review.googlesource.com/c/tools/+/221877 Reviewed-by: Dmitri Shuralyov <[email protected]>
Thank you for filing this bug @alvaroaleman, and welcome to the Go project! Thanks for the work on it @dmitshur and @jba! I saw that @jba's CL https://go-review.googlesource.com/c/tools/+/221877/ added some clarification. Is there more that we could do for this issue? Thanks. |
Hey thanks for the ping, I think this can be considered solved, thanks for the help everyone! |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
asumming the playground uses the latest release, yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/TeXqOwa0wGM
What did you expect to see?
Successful compilation
What did you see instead?
second argument to errors.As must be a pointer to an interface or a type implementing error
The workaround is to pass in a double pointer, but that is super unintuitive: https://play.golang.org/p/kehktd-1b5t
The text was updated successfully, but these errors were encountered: