x/blog: add non-pointer note for errors.As example
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 <jba@google.com>
Reviewed-by: Kevin Burke <kev@inburke.com>
Trust: Jean de Klerk <deklerk@google.com>
Trust: Damien Neil <dneil@google.com>
diff --git a/content/go1.13-errors.article b/content/go1.13-errors.article
index b0f445d..f5ffd84 100644
--- a/content/go1.13-errors.article
+++ b/content/go1.13-errors.article
@@ -135,6 +135,7 @@
// Similar to:
// if e, ok := err.(*QueryError); ok { … }
var e *QueryError
+ // Note: *QueryError is the type of the error.
if errors.As(err, &e) {
// err is a *QueryError, and e is set to the error's value
}
@@ -160,6 +161,10 @@
`Unwrap` method. It is usually better to use `errors.Is` or `errors.As`,
however, since these functions will examine the entire chain in a single call.
+Note: although it may feel odd to take a pointer to a pointer, in this case it
+is correct. Think of it instead as taking a pointer to a value of the error
+type; it so happens in this case that the returned error is a pointer type.
+
### Wrapping errors with %w
As mentioned earlier, it is common to use the `fmt.Errorf` function to add additional information to an error.