Opened https://github.com/golang/go/issues/47528
On Monday, August 2, 2021 at 6:30:04 PM UTC+2 Andreas Götz wrote: > > 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. > > Will do. It could also be a vet check to highlight errors.As used on > sentinel errors which is most likely wrong as I demonstrated. > > On Monday, August 2, 2021 at 10:09:48 AM UTC+2 axel.wa...@googlemail.com > wrote: > >> On Mon, Aug 2, 2021 at 10:02 AM cpu...@gmail.com <cpu...@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...@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/e97882af-4c3f-4a0b-ab54-277cc53ae1b0n%40googlegroups.com.