Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

err != nil when err == nil with custom error type #35292

Closed
jamoozy opened this issue Oct 31, 2019 · 3 comments
Closed

err != nil when err == nil with custom error type #35292

jamoozy opened this issue Oct 31, 2019 · 3 comments

Comments

@jamoozy
Copy link

jamoozy commented Oct 31, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13.3 linux/amd64

Though I think this works on all recent versions ... ?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jamoozy/.cache/go-build"
GOENV="/home/jamoozy/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/jamoozy/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/jamoozy/lib/go1.13.3"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/jamoozy/lib/go1.13.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/jamoozy/silo/go/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build549669925=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/wYWX4cDk3rc

What did you expect to see?

I expected err == nil to return true.

What did you see instead?

I saw err == nil: false in the output. I suspect this has to do with what is mentioned in the laws of reflection: that instead of err having a pure nil value, it's pointing to the (value, type) tuple (nil, *Err). However, I don't understand why this would be desirable.

Thank you for your time!

@gertcuykens
Copy link
Contributor

gertcuykens commented Oct 31, 2019

Correct because, interface has type and value
https://golang.org/doc/faq#nil_error

If you look at it from a interface perspective it just follows the rules as all other interfaces

@jamoozy
Copy link
Author

jamoozy commented Oct 31, 2019

Many thanks for the response! I'm still curious why this is the desired behavior, though. What kind of code relies on nil comparisons acting this way?

@ianlancetaylor
Copy link
Member

You may want to look at #22729.

Basically, nil is overloaded. It is the zero value of both pointer types and interface types (and also map, slice, function, and channel types). It is entirely reasonable to store a nil pointer into a variable of interface type. Such a value is no longer the zero value of its type, so comparing it with nil (this time the nil interface value, not the nil pointer value) must be false.

@golang golang locked and limited conversation to collaborators Oct 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants