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

Pivot map[key] reversal doesn't revert the correct value #39242

Closed
goldsky opened this issue May 25, 2020 · 4 comments
Closed

Pivot map[key] reversal doesn't revert the correct value #39242

goldsky opened this issue May 25, 2020 · 4 comments

Comments

@goldsky
Copy link

goldsky commented May 25, 2020

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

$ go version
go1.14.2 darwin/amd64

Does this issue reproduce with the latest release?

If play.golang.org has the latest release, then yes.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/admin/Library/Caches/go-build"
GOENV="/Users/admin/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/usr/local/var/www/htdocs/gopath"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/c9/gwddgxzn4gqf_4qr4gdf68yw0000gn/T/go-build273093480=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I am trying to flatten inner slice by attaching the outer slice, then reverse the output of flatten the outer slice by attaching inner slice

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

Inner slices

map[id:1]
map[id:2]
map[id:3]

Outer slice
[map[id:A inner:[map[id:1] map[id:2] map[id:3]]]]

Pivot Inner
[map[id:1 outer:map[id:A]] map[id:2 outer:map[id:A]] map[id:3 outer:map[id:A]]]

What did you expect to see?

Pivot Outer
[map[id:A inner:map[id:1]] map[id:A inner:map[id:2]] map[id:A inner:map[id:3]]]

What did you see instead?

Pivot Outer
[map[id:A inner:map[id:3]] map[id:A inner:map[id:3]] map[id:A inner:map[id:3]]]

@seankhliao
Copy link
Member

https://github.com/golang/go/wiki/Range#gotchas

@goldsky
Copy link
Author

goldsky commented May 25, 2020

if you run the playground

		// dump row
		fmt.Println(pivotInnerMap)
		outerX["inner"] = pivotInnerMap

returns

map[id:1]
map[id:2]
map[id:3]

but when appended the value is lost

@seankhliao
Copy link
Member

That is the point of the link. Ranges reuse the same variable for all iterations, and maps are reference types so your code all points to the same map.

@seankhliao
Copy link
Member

see https://github.com/golang/go/wiki/Questions for places to ask questions

@goldsky goldsky closed this as completed May 25, 2020
@golang golang locked and limited conversation to collaborators May 25, 2021
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