Navigation Menu

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

append maintains reference rather than value? #42870

Closed
tyleradams opened this issue Nov 28, 2020 · 3 comments
Closed

append maintains reference rather than value? #42870

tyleradams opened this issue Nov 28, 2020 · 3 comments

Comments

@tyleradams
Copy link

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

$ go version
go version go1.15.5 linux/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="/home/cowboy/.cache/go-build"
GOENV="/home/cowboy/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/cowboy/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/cowboy/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build536029970=/tmp/go-build -gno-record-gcc-switches"

What did you do?

In: https://github.com/tyleradams/json-toolkit/tree/master/debug
Ran:

go run ../json-diff.go o.json u.json

What did you expect to see?

[{"leftValue":1.807738195866628,"path":[0,"result","analysis","tTest1Samp":[0,"result","tTest1Samp"],"rightValue":"(1.807738195866628,)"}]

What did you see instead?

The result is non-deterministic and the last element of path changes, here from tTest1Samp to wins:
[{"leftValue":1.807738195866628,"path":[0,"result","analysis","wins"]},{"path":[0,"result","tTest1Samp"],"rightValue":"(1.807738195866628,)"}]

Any leads?

I narrowed down the problem to lines 79,89
for key := range map1 { _, keyInMap2 := map2[key] if keyInMap2 { diff = append(diff, compareObject(append(path, key), map1[key], map2[key])...) } else { diff = append(diff, map[string]interface{}{ "path": append(path, key), "leftValue": map1[key], }) } }
path is maintaining a reference to key instead of a value and as key iterates through map1, the value of a stored append(path, key) changes over the iterations. Even if I try something like
new_key := string(key) "path": append(path, new_key)
the mutation still occurs.

From the string and append documentations it doesn't seem like this should be the case, so that's why there may be an underlying issue in go.

@egonelbre
Copy link
Contributor

This is the expected behavior. https://golang.org/pkg/builtin/#append

When the underlying slice has enough capacity it will be reused. You can read more about it in:

@egonelbre
Copy link
Contributor

As a sidenote, I recommend running go vet on the codebase:

.\json-diff.go:129:48: conversion from Kind (uint) to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)
.\json-diff.go:129:76: conversion from Kind (uint) to string yields a string of one rune, not a string of digits (did you mean fmt.Sprint(x)?)

@tyleradams
Copy link
Author

Ah, I see, sorry to bother.

@golang golang locked and limited conversation to collaborators Nov 28, 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