Skip to content

Some inconsistent Values on Slice output #34428

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
shani1998 opened this issue Sep 20, 2019 · 2 comments
Closed

Some inconsistent Values on Slice output #34428

shani1998 opened this issue Sep 20, 2019 · 2 comments

Comments

@shani1998
Copy link

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

$ go version - go1.13 windows/amd64

Does this issue reproduce with the latest release?

yes

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

amd64

go env Output
$ go env
go version
go version go1.13 windows/amd64

C:\Go\src\myPrograms\study-Go>go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\shpathak\AppData\Local\go-build
set GOENV=C:\Users\shpathak\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\shpathak\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=C:\Go\src\go.mod
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\shpathak\AppData\Local\Temp\go-build079362430=/tmp/go-build -gno-record-gcc-switches

What did you do?

When I execute the below programme , I am getting the output of variable x is x: [1 2 3 10]
But It should be x: [1 2 3 4]

/******
package main

import (
"fmt"
)

func main() {
//case 1
a := []int{}
a = append(a, 1)
a = append(a, 2)
b := append(a, 3)
c := append(a, 4)
fmt.Println("a: ", a, "\nb: ", b, "\nc: ", c)

//case 2
a = append(a, 3)
x := append(a, 4)
y := append(a, 10)

fmt.Println("a: ", a, "\nx: ", x, "\ny: ", y)

}
/**************************/

What did you expect to see?

value of X should be x: [1 2 3 4].
But it is printing x: [1 2 3 10] which is wrong

What did you see instead?

x: [1 2 3 10]

@agnivade
Copy link
Contributor

Hi @shani1998,
This is WAI. The underlying array is the same. Please see https://blog.golang.org/go-slices-usage-and-internals and https://blog.golang.org/slices.

Unlike other projects, we do not use the issue tracker for questions such as these. It is only used for bugs and feature proposals. I will close this issue, but please feel free to ask it in any of these forums below:

Thanks

@av86743
Copy link

av86743 commented Sep 20, 2019

Detailed in #33111.

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