Thank you for the clarification.

On Sat, Nov 5, 2022 at 10:37 PM Axel Wagner <axel.wagner...@googlemail.com>
wrote:

> To be clear: The recommendation is *not* to compare strings. The
> recommendation is to compare errors by identity and not consider errors
> created by different packages to be equal.
>
> If you desperately need your error to be considered "the same" as another,
> the most correct way would be to implement `Is(other error) bool`, e.g.
>
> type notQuiteEOF struct {}
> func (notQuiteEOF) Error() string {
>     return "not quite EOF, but almost"
> }
> func (notQuiteEOF) Is(other error) bool {
>     return other == io.EOF
> }
>
> This allows errors to be considered "the same" under `errors.Is`, while
> not actually being the same and it's probably the recommended mechanism to
> "imitate" an error from a different package.
>
> On Sat, Nov 5, 2022 at 6:00 PM Nikhilesh Susarla <nikhilesh1...@gmail.com>
> wrote:
>
>> The error is coming from other package. So, then have to compare strings.
>> I guess
>>
>> On Sat, 5 Nov 2022 at 22:28, Sean Foley <seancfo...@gmail.com> wrote:
>>
>>> If the error is created by your code, then just reuse the same one.
>>>
>>> See
>>> https://cs.opensource.google/go/go/+/refs/tags/go1.19.3:src/io/io.go;drc=90b40c0496440fbd57538eb4ba303164ed923d93;l=44
>>>
>>> If the error is created by code other than your own, and that code does
>>> not reuse the same error, then compare strings.
>>>
>>>
>>> On Sat, Nov 5, 2022 at 12:39 PM Nikhilesh Susarla <
>>> nikhilesh1...@gmail.com> wrote:
>>>
>>>> Oh I see.
>>>> What is the best way to compare errors?
>>>> Here in the above example I can do e.Error() == ErrNotFound.Error() //
>>>> which returns true
>>>> Is there any other way rather than string comparison ?
>>>>
>>>> Thank you
>>>>
>>>> On Sat, Nov 5, 2022 at 10:03 PM Axel Wagner <
>>>> axel.wagner...@googlemail.com> wrote:
>>>>
>>>>> Oh and this behavior is documented, of course:
>>>>> https://pkg.go.dev/errors#New
>>>>>
>>>>> On Sat, Nov 5, 2022 at 5:32 PM Axel Wagner <
>>>>> axel.wagner...@googlemail.com> wrote:
>>>>>
>>>>>> Every invocation of `errors.New` returns a new unique error value,
>>>>>> even if the same error text is used.
>>>>>> That is intentional. It would be confusing, if package A chose the
>>>>>> same error sentinel text as package B and suddenly their sentinels 
>>>>>> compare
>>>>>> as equal.
>>>>>> If you want error identity between values, you have to actually copy
>>>>>> the error value (or implement your own, which may very well not do it 
>>>>>> this
>>>>>> way).
>>>>>>
>>>>>> On Sat, Nov 5, 2022 at 5:29 PM Nikhilesh Susarla <
>>>>>> nikhilesh1...@gmail.com> wrote:
>>>>>>
>>>>>>>
>>>>>>> Same interface comparison
>>>>>>>
>>>>>>> https://play.golang.com/p/9hHlTDosYzz
>>>>>>>
>>>>>>> Why is the equals too still returning false?
>>>>>>>
>>>>>>> Any more details on this?
>>>>>>>
>>>>>>> Thank you
>>>>>>>
>>>>>>> --
>>>>>>> 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/ee3d53d3-d3c8-4a3b-801a-af6060316e3an%40googlegroups.com
>>>>>>> <https://groups.google.com/d/msgid/golang-nuts/ee3d53d3-d3c8-4a3b-801a-af6060316e3an%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/CAMt-viXeFOgh42DtvD4cJiEvhUDR-PaD8GvOEDfuytLdeKE4GQ%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/golang-nuts/CAMt-viXeFOgh42DtvD4cJiEvhUDR-PaD8GvOEDfuytLdeKE4GQ%40mail.gmail.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/CAMt-viUCSAtcZSSf7XeQoAf%3D4z%3D0bqd77jTUceSW%3DNoFtZk9zA%40mail.gmail.com.

Reply via email to