On Tue, Dec 19, 2017 at 8:54 AM, Sasan Rose <sasan.r...@gmail.com> wrote:
> Please take a look at https://play.golang.org/p/BL4LUGk-lH

solutionNew = make([]int, 0)
solutionNew = append(solution, 2)

Is a strange thing to do, you create a new slice using make([]int, 0)
and then never use it.
perhaps you wanted to use copy()?


The problem you're encountering is slices can share backing arrays.
Each time you append to the slice called 'solution', you're modifying
the same backing array.

In your code:

/*You append 2 to solution creating a slice that points to the same
memory as 'solution'*/
 solutionNew = append(solution, 2)

/* You append this slice to a slice called slice */
 slice = append(slice, solutionNew)

/* You append a 4 to the same slice called 'solution' which shares
memory with slice you appended to your slice called 'slice' thus you
override the 2 you append to 'solution' with a 4*/
 solutionNew = append(solution, 4)

/* You append the slice to 'slice' so now there are two slices in
'slice' that refer to the same memory and thus have the same value */
 slice = append(slice, solutionNew)


- Jesse

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to