Skip to content

Commit 989b9fc

Browse files
committed
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 <[email protected]> Reviewed-by: Kevin Burke <[email protected]> Trust: Jean de Klerk <[email protected]> Trust: Damien Neil <[email protected]>
1 parent a311423 commit 989b9fc

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

content/go1.13-errors.article

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ The `As` function tests whether an error is a specific type.
135135
// Similar to:
136136
// if e, ok := err.(*QueryError); ok { … }
137137
var e *QueryError
138+
// Note: *QueryError is the type of the error.
138139
if errors.As(err, &e) {
139140
// err is a *QueryError, and e is set to the error's value
140141
}
@@ -160,6 +161,10 @@ result of calling an error's `Unwrap` method, or `nil` when the error has no
160161
`Unwrap` method. It is usually better to use `errors.Is` or `errors.As`,
161162
however, since these functions will examine the entire chain in a single call.
162163

164+
Note: although it may feel odd to take a pointer to a pointer, in this case it
165+
is correct. Think of it instead as taking a pointer to a value of the error
166+
type; it so happens in this case that the returned error is a pointer type.
167+
163168
### Wrapping errors with %w
164169

165170
As mentioned earlier, it is common to use the `fmt.Errorf` function to add additional information to an error.

0 commit comments

Comments
 (0)