You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I assign y slice variable from x sub-slice.
I try to append 30 to y variable. I expect to append y variable only. but append y and x.
perhaps, y and x is look like watch some address.
In this case, should be pass by value, but it's call by pointer.
package main
import"fmt"funcmain() {
x:= []int{1, 2, 3, 4}
y:=x[:2]
fmt.Println("x:", x) // x: [1 2 3 4]fmt.Println("y:", y) // y: [1 2]y=append(y, 30)
fmt.Println("x:", x) // x: [1 2 30 4] ← I think x should be [1 2 3 4].fmt.Println("y:", y) // y: [1 2 30]
}
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?intel MacOS Monterey version 12.6, yes I use go env.
go env
OutputWhat did you do?
I assign
y
slice variable fromx
sub-slice.I try to append
30
toy
variable. I expect to appendy
variable only. but appendy
andx
.perhaps, y and x is look like watch some address.
In this case, should be pass by value, but it's call by pointer.
play : https://go.dev/play/p/HWwI5WCjDXm
What did you expect to see?
Of course, I know how to fix this bug, should use
copy
.play : https://go.dev/play/p/IayY-PdoM0l
The text was updated successfully, but these errors were encountered: