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

"var" and ":=" declaration treated differently for slices in reflect.DeepEqual() #43857

Closed
mqrc81 opened this issue Jan 22, 2021 · 1 comment
Closed

Comments

@mqrc81
Copy link

mqrc81 commented Jan 22, 2021

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

$ go version go1.15.5 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="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ms/Library/Caches/go-build"
GOENV="/Users/ms/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ms/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ms/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/ms/sdk/go1.15.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/ms/sdk/go1.15.5/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ms/Projects/Test-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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8s/qkvh49xx19x8qnd4hv5wlrlw0000gn/T/go-build946297595=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here's the code: https://play.golang.org/p/yfTT4rvR4cb

When testing my Go web application, I realized that var arr1 []someStruct and arr2 := []someStruct{} are treated differently with reflect.DeepEqual(arr1, arr2). This is very misleading, since they're treated the same for single struct (not slices).

What did you expect to see?

I expected both slices of an empty struct to be exactly the same, so reflect.DeepEqual() => true.

What did you see instead?

As seen in the code, the var arr1 []someStruct and arr2 := []someStruct{} declarations behave differently in the reflect.DeepEqual() function.

@seankhliao
Copy link
Member

They are different things, a nil slice and empty slice

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Jan 22, 2022
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