4 ms·
I was actually referring to the implementation of errors.Is, which uses string comparison internally if you use the error type returned by errors.New and a type
by packetlost 1y ago
I was actually referring to the implementation of errors.Is, which uses string comparison internally if you use the error type returned by errors.New and a type cast or switch if you use a custom type (or the cases where the stdlib defines a custom error type).
- kiitos 1y agoerrors.Is definitely doesn't use string comparison internally: https://cs.opensource.google/go/go/+/refs/tags/go1.24.3:src/errors/wrap.go;l=27-78 https://cs.opensource.google/go/go/+/refs/tags/go1.24.3:src/...
- masklinn 1y agoOf course it does. What do you think an errors.New contains and how do you figure it compares by value when checking placeholder errors? The answer is that errors.New just wraps the error message in an errorString struct, and the second line of `is` is a string comparison.
- kiitos 1y agoSee for yourself: https://go.dev/play/p/Q7IPuPS5CwQ https://go.dev/play/p/Q7IPuPS5CwQ
- masklinn 1y agoOh hell, I always forget equality doesn't go through pointers in go, you're right. And as you demonstrate I could have tested it easily enough to confirm I was reading the code correctly...
- Yoric 1y agoMy bad, I misunderstood your previous comment.