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

bug report : builtin function "append" Malfunction #32127

Closed
NikoKVCS opened this issue May 18, 2019 · 2 comments
Closed

bug report : builtin function "append" Malfunction #32127

NikoKVCS opened this issue May 18, 2019 · 2 comments

Comments

@NikoKVCS
Copy link

NikoKVCS commented May 18, 2019

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

$ go version
go version go1.10.3 linux/amd64

Does this issue reproduce with the latest release?

not sure

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/niko/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/niko/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build605005347=/tmp/go-build -gno-record-gcc-switches"

What did you do?

play.golang.org link is below:
https://play.golang.org/p/SAooamwMWuc

line1: rear := result[id:]
line2: front := result[:id]
line3: temp := append(front, item)
line4: result = append(temp, rear...)

the bug of "append" is : before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 4, the rear equals to [-3, 1, 3] for no reason which is really weird

What did you expect to see?

the expect result is : after it execute the line3, the variable rear won't change. and the codes ' purpose is to insert a item

What did you see instead?

before it execute the line 3, the rear equals to [-4, 2, 2], after it execute the line 3, the rear equals to [-3, 1, 3] for no reason which is really weird.

@martisch
Copy link
Contributor

martisch commented May 18, 2019

That looks to be how append is supposed to work (not a bug) and is documented because it reuses the slices backing array up to capacity.

A copy for rear can be made if rear should not change due to other slices operations modifying the same backing array. Alternatively for this very specific code the capacity can be limited so append creates a new backing array for temp by using front := result[:id:id].

https://golang.org/ref/spec#Appending_and_copying_slices

@ianlancetaylor
Copy link
Contributor

See also https://blog.golang.org/slices.

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