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

reflect: reflect.Value.IsZero results false positive #34108

Closed
changkun opened this issue Sep 5, 2019 · 3 comments
Closed

reflect: reflect.Value.IsZero results false positive #34108

changkun opened this issue Sep 5, 2019 · 3 comments

Comments

@changkun
Copy link
Member

changkun commented Sep 5, 2019

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

$ go version
go version go1.13 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/changkun/Library/Caches/go-build"
GOENV="/Users/changkun/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/changkun/dev/golang"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/changkun/dev/sched/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/fr/hk6cn7m95bn7n0p054d35fc80000gp/T/go-build674759185=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

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

What did you expect to see?

The expected output is true because reflect.New creates a zero value

What did you see instead?

Actual output is false.

@mvdan
Copy link
Member

mvdan commented Sep 5, 2019

reflect.New returns a pointer to a new value, so it's by definition non-nil, and non-zero.

@changkun
Copy link
Member Author

changkun commented Sep 5, 2019

@mvdan This is quite non-intuitive.
What is the decisive reason for reflect.New that creates a pointer to the value?
What is the correct way to check the newly created (actual) value by reflect.New is a zero value?

@mvdan
Copy link
Member

mvdan commented Sep 5, 2019

See the docs at https://golang.org/pkg/reflect/#New. It works exactly like new, and has been like that for years, so we can't change it even if we wanted to.

If you want to check if the data behind the pointer is zero, do ptr.Elem().IsZero(). You should check the documentation to see why that works.

In the future, please try asking questions about reflect in the forums or Slack before filing bugs. The package takes a while to master, so most problems tend to not be bugs in Go itself.

@mvdan mvdan closed this as completed Sep 5, 2019
@golang golang locked and limited conversation to collaborators Sep 4, 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

3 participants