Skip to content

Bug with []byte struct field at append slice in for range loop #35340

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

Closed
jekaspekas opened this issue Nov 4, 2019 · 1 comment
Closed

Bug with []byte struct field at append slice in for range loop #35340

jekaspekas opened this issue Nov 4, 2019 · 1 comment

Comments

@jekaspekas
Copy link

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

$ go version
go version go1.13.4 windows/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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\jekaspekas\AppData\Local\go-build
set GOENV=C:\Users\jekaspekas\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\jekaspekas\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\JEKASP~1\AppData\Local\Temp\go-build176995138=/tmp/go-build -gno-record-gcc-switches

What did you do?

Link on play.golang.org

What did you expect to see?

testList 0: &{F:[4 4] B:[4 4]}
testList 1: &{F:[5 5] B:[5 5]}
testList 2: &{F:[0 0] B:[0 0]}
testList 3: &{F:[1 1] B:[1 1]}
testList 4: &{F:[2 2] B:[2 2]}
testList 5: &{F:[3 3] B:[3 3]}

What did you see instead?

testList 0: &{F:[4 4] B:[3 3]}
testList 1: &{F:[5 5] B:[3 3]}
testList 2: &{F:[0 0] B:[3 3]}
testList 3: &{F:[1 1] B:[3 3]}
testList 4: &{F:[2 2] B:[3 3]}
testList 5: &{F:[3 3] B:[3 3]}
@ALTree
Copy link
Member

ALTree commented Nov 4, 2019

Hi.

This is working as intended. The range variables (in your case, v) are not re-allocated at each loop, so if you capture them by pointer (which you are doing, when you store v[:]), you'll fill the list with the same element again and again.

To fix this, either iterate on the indices (like in your second loop), or add a v := v at the top of the range loop.

I'm closing here, since this is not a bug. If you have further questions, please see the Questions wiki page. Thanks!

@ALTree ALTree closed this as completed Nov 4, 2019
@golang golang locked and limited conversation to collaborators Nov 3, 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