On Mon, Aug 2, 2021 at 10:02 AM cpu...@gmail.com <cpui...@gmail.com> wrote:

> Consider this example: https://play.golang.org/p/16cU0kc8Lku, basically
>
> var Err = errors.New("sentinel")
> err := errors.New("foo")
> if errors.As(err, &Err) {
>   fmt.Println("why?")
> }
>
> I'm wondering why this matches the sentinel error
>

Because both are of type `*errors.errorString`, so the types match and it
copies over the value.
Arguably, `errors.errorString` should implement `As()` and return `false`,
unless the pointers match. I suggest filing an issue.


> or rather how to properly use sentinel errors.
>

Use `errors.Is`, instead of `errors.As`.


> However, if thats the case, how should I construct a sentinel error that
> would be usable with errors.As? I don't want to rely on errors.Is as errors
> could be wrapped.
>

And? They can be wrapped for `As` as well. `errors.As` is only useful if
you want to get additional information out of the error (e.g. it's useful
for use with `os.PathError`). By definition, it's pointless to use it with
a sentinel error.


> So to "break" the assignability- would that mean that I'd need to define
> sentinel errors as e.g. struct types?
>
> Seem's I'm still lacking the level of understanding for go errors that I'd
> aspire to :/
>
> Cheers,
> Andi
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/4d8969b4-dd5a-4834-893b-3e01174570bfn%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/4d8969b4-dd5a-4834-893b-3e01174570bfn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfGyjtNg-vGCvS95Pt0_iy_-yVWhr8TcKixhyvvMVkf_0g%40mail.gmail.com.

Reply via email to