Go version
go version go1.24.4 linux/amd64
Output of go env
in your module/workspace:
AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/jmymay/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/jmymay/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build251118401=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/jmymay/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/jmymay/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.24'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/jmymay/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.24/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
Attempted to use errors.Is
on nested error struct which is not a comparable type:
package main
import (
"errors"
"fmt"
)
type NestedError struct {
Err error
}
func (e NestedError) Error() string {
return e.Err.Error()
}
type ErrWithSlice struct {
Slice []string
}
func (e ErrWithSlice) Error() string {
return "ErrWithSlice"
}
func main() {
if errors.Is(
NestedError{Err: ErrWithSlice{[]string{"a", "b"}}},
NestedError{Err: ErrWithSlice{[]string{"a", "b"}}},
) {
fmt.Println("a is b")
} else {
fmt.Println("a is not b")
}
}
What did you see happen?
$ go run error_is_example.go
panic: runtime error: comparing uncomparable type main.ErrWithSlice
What did you expect to see?
a is not b
should be printed.
Note this still behaves correctly if ErrWithSlice is passed directly to errors.Is
, as opposed to nested.
Comment From: jmymay
Possibly similar past issue: https://github.com/golang/go/issues/54709
Comment From: gabyhelp
Related Issues
- errors: Is should not panic #54709 (closed)
- panic: runtime error: comparing uncomparable type when error has interface field populated by a slice #36115 (closed)
- x/xerrors: Uncomparable error type causes runtime panic #31841 (closed)
- errors: Is(nil) behaves unexpectedly #40442 (closed)
- errors: errors.Join does not skip nil error interface implementations #70626 (closed)
- Function nesting calls a function that returns a pointer that implements the error, the returned error is always not equal to nil #43767 (closed)
- everything: fail to check err == nil when assigning explicit type to `error` #56930 (closed)
- os/exec: ExitError does not conform to errors best practice via errors.Is() #51120 (closed)
- Inconsistency in Comparing Two error Values #63798 (closed)
- proposal: errors: allow implementing err.Is(err) == false #40675 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: neild
Note that errors.Is
can only panic if the target error is not comparable.
That is, if err == target
panics, then errors.Is(err, target)
also panics, as does errors.Is(errWrappingTarget, target)
. If err == err
does not panic, then errors.Is(x, err)
does not panic. (Perhaps I'm missing something and there are cases where this isn't true; if so, I'd appreciate it if someone could point them out.)
Since errors.Is(err, target)
is essentially err == target
but across the entire tree of err
, I think this behavior is fairly reasonable. If we had a cheap way to avoid the panic, we might use it, but we don't.
So perhaps errors.Is
should just better document that it can panic if handed a target that is not comparable.
Comment From: gopherbot
Change https://go.dev/cl/686175 mentions this issue: errors: document that Is can panic on noncomparable errors