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 have a base slice with some elements. And I need 2 slices with the same base elements, and some different elements at the end of them, so I used append to add those elements to the base array 2 times, assigning each of them to a new different variable. Something like this:
a := append(base, elem1)
b := append(base, elem2)
The content of a and b after assigning b is the same in both slices. This changes depending on the number .
Here's an example reproducing the error (with a link to the playground with the same code).
package main
import "fmt"
func main() {
lim := 3
a := []int{}
for i := 0; i < lim; i++ {
a = append(a, i)
}
b := append(a, 20)
fmt.Println(b)
c := append(a, 30)
fmt.Println(b)
fmt.Println(c)
}
when changing lim value to 2, the behavior is different, a and b have the values I would expect:
What did you expect to see?
the same behavior when assigning the new variables, regardless of the number of elements in the base slice.
with lim:= 2
[0 1 20]
[0 1 20]
[0 1 30]
with lim:=3
[0 1 2 20]
[0 1 2 20]
[0 1 2 30]
What did you see instead?
with lim:= 2
[0 1 20]
[0 1 20]
[0 1 30]
with lim:=3
[0 1 2 20]
[0 1 2 30]
[0 1 2 30]
depending on the number of elements, the first variable I assign takes the value I appended first or second. This is confusing to me. Is this behaviour expected?
The text was updated successfully, but these errors were encountered:
sipwarriper
changed the title
cmd/compile: incosistent behaviour when assigning slices from a base slice.
cmd/compile: incosistent behaviour when assigning slices from a base slice with append
Nov 30, 2022
This is normal slice behavior. They initially share the same backing array in memory and thus affect each other, but if the data exceeds the capacity, append allocates a new backing array with twice the capacity and copies the data. Now the slices have different backing arrays in different memory locations and do no longer affect each other.
You can see this by logging the memory addresses and capacities of the slices:
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
)?go env
OutputWhat did you do?
I have a base slice with some elements. And I need 2 slices with the same base elements, and some different elements at the end of them, so I used append to add those elements to the base array 2 times, assigning each of them to a new different variable. Something like this:
The content of
a
andb
after assigningb
is the same in both slices. This changes depending on the number .Here's an example reproducing the error (with a link to the playground with the same code).
https://go.dev/play/p/5IeuvhEOM13
when changing
lim
value to 2, the behavior is different,a
andb
have the values I would expect:What did you expect to see?
the same behavior when assigning the new variables, regardless of the number of elements in the base slice.
with
lim:= 2
with
lim:=3
What did you see instead?
with
lim:= 2
with
lim:=3
depending on the number of elements, the first variable I assign takes the value I appended first or second. This is confusing to me. Is this behaviour expected?
The text was updated successfully, but these errors were encountered: