-
Notifications
You must be signed in to change notification settings - Fork 18k
blog: unintuitive code in errors post example #35361
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
Also, sorry, I'm sure the pros and cons of this design were covered in the spec and that there are good reasons for doing it this way, I am not saying it's a bad design but we could do a better job of communicating it to folks. |
FWIW this surprised me at first too. I had to think about it for a bit to realize why it makes sense this way. (My rough reasoning is something like: we already have a I think the reason it feels weird at first is that the code looks similar to a common pattern used in JSON unmarshaling and similar contexts, where you might well do (supposing the QueryError could unmarshal itself from JSON):
In that case, there was no |
Related to #37625 . |
The way I understood this is that the second argument to errors.As() (aptly named target), is an output argument. An output argument needs to be passed a pointer to the target, because you want the function to assign to it. And the type of the output (error value) is *QueryError, so you clearly need to pass a pointer to a *QueryError. I think one problem in the blog post is that the full function signature wasn't shown ( |
Change https://golang.org/cl/252825 mentions this issue: |
Change https://golang.org/cl/252877 mentions this issue: |
Since this is a very high SEO article and it's a copy-pasteable block, it gets copy pasted without much thought, which leads to problems when the error is a concrete type / interface / etc. This CL adds a small note that users should watch out for the case when the error type is not a pointer. Fixes golang/go#35361 Change-Id: Ibbd950c2a73a5f30cdab3517e042f69465adff97 Reviewed-on: https://go-review.googlesource.com/c/blog/+/252877 Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Kevin Burke <[email protected]> Trust: Jean de Klerk <[email protected]> Trust: Damien Neil <[email protected]> X-Blog-Commit: 989b9fc7a2626e387b19079b084798a016de1018
Since this is a very high SEO article and it's a copy-pasteable block, it gets copy pasted without much thought, which leads to problems when the error is a concrete type / interface / etc. This CL adds a small note that users should watch out for the case when the error type is not a pointer. Fixes golang/go#35361 Change-Id: Ibbd950c2a73a5f30cdab3517e042f69465adff97 Reviewed-on: https://go-review.googlesource.com/c/blog/+/252877 Reviewed-by: Jonathan Amsterdam <[email protected]> Reviewed-by: Kevin Burke <[email protected]> Trust: Jean de Klerk <[email protected]> Trust: Damien Neil <[email protected]> X-Blog-Commit: 989b9fc7a2626e387b19079b084798a016de1018
I am reading about how to use the new Go errors code. In this blog post, there's the following example:
My initial read was that this can't be right - it's taking the address of a
*QueryError
, which gives a**QueryError
, which a) does not match the type of the "Similar to" immediately above it, and b) a pointer to a pointer is almost never what you want in Go.However I wrote some tests and yeah a
**QueryError
is actually what we want here:The blog post doesn't really mention this weirdness at all though.
I presume some folks like me are going to out of instinct remove either the
*
or the&
in order to get one layer of nesting and then be confused when that's not correct. It might be good to add a note that, yes, this is the correct thing to do.The text was updated successfully, but these errors were encountered: