Hi,
I'm trying to convert a slice of arrays into a slice of slices but I
encountered some really weird behavior.
Every value in my copy somehow becomes the last value that was copied as if
the slices suddenly started sharing the same backing array even though they
were originally derived from different arrays.
The only way I can explain is with this example code.
http://play.golang.org/p/gTmh73Hcjp
package main
import "fmt"
func main() {
var original [][1]byte
original = append(original, [1]byte{1})
original = append(original, [1]byte{2})
fmt.Println("ORIGINAL", original)
var loopcopy [][]byte
for _, array := range original {
slice := array[:]
fmt.Println("COPY IN LOOP", slice)
loopcopy = append(loopcopy, slice)
}
fmt.Println("LOOP COPY", loopcopy)
var unrolled [][]byte
fmt.Println("COPY UNROLLED", original[0][:])
unrolled = append(unrolled, original[0][:])
fmt.Println("COPY UNROLLED", original[1][:])
unrolled = append(unrolled, original[1][:])
fmt.Println("UNROLLED COPY", unrolled)
}
I expect the output to be
ORIGINAL [[1] [2]]
COPY IN LOOP [1]
COPY IN LOOP [2]*LOOP COPY [[1] [2]]*
COPY UNROLLED [1]
COPY UNROLLED [2]
UNROLLED COPY [[1] [2]]
but instead I get
ORIGINAL [[1] [2]]
COPY IN LOOP [1]
COPY IN LOOP [2]*LOOP COPY [[2] [2]]*
COPY UNROLLED [1]
COPY UNROLLED [2]
UNROLLED COPY [[1] [2]]
Some how the copy made in the loop contains 2,2 instead of 1,2
I'm using go 1.0.3 locally. When I press the run button in go playground I also see the unexpected behavior.
I was also able to reproduce the behavior with a local build with version "devel +5529ef3e8a9b Wed Dec 12 16:43:54 2012 +0100"
Is this a real issue or have I done something dumb.
--