Problem using append

hi

last night i tried to develop something when a weird thing happened

I found where the problem came from and isolated it:

package main 

import "fmt"

func main(){
    paths := [][]int{[]int{0}}
    path :=[]int{0}
    fmt.Println("paths :",paths,", path :",path)
    
    for k:=0;k<5;k++{
        tmp:= append(path,0)
        
        fmt.Println(paths,"+",tmp,"//",path)
        paths = append(paths,tmp)
        fmt.Println("->",paths,"//",path)
        tmp2 := append(path,1)
        fmt.Println(paths,"+",tmp2,"//",path)
        paths = append(paths,tmp2)
        fmt.Println("->",paths,"//",path)
        
        fmt.Println("\n")
        paths = paths[1:]
        path = paths[0]
    }
}

output were :

paths : [[0]] , path : [0]
[[0]] + [0 0] // [0]
-> [[0] [0 0]] // [0]
[[0] [0 0]] + [0 1] // [0]
-> [[0] [0 0] [0 1]] // [0]

[[0 0] [0 1]] + [0 0 0] // [0 0]
-> [[0 0] [0 1] [0 0 0]] // [0 0]
[[0 0] [0 1] [0 0 0]] + [0 0 1] // [0 0]
-> [[0 0] [0 1] [0 0 0] [0 0 1]] // [0 0]

[[0 1] [0 0 0] [0 0 1]] + [0 1 0] // [0 1]
-> [[0 1] [0 0 0] [0 0 1] [0 1 0]] // [0 1]
[[0 1] [0 0 0] [0 0 1] [0 1 0]] + [0 1 1] // [0 1]
-> [[0 1] [0 0 0] [0 0 1] [0 1 0] [0 1 1]] // [0 1]

[[0 0 0] [0 0 1] [0 1 0] [0 1 1]] + [0 0 0 0] // [0 0 0]
-> [[0 0 0] [0 0 1] [0 1 0] [0 1 1] [0 0 0 0]] // [0 0 0]
[[0 0 0] [0 0 1] [0 1 0] [0 1 1] [0 0 0 1]] + [0 0 0 1] // [0 0 0]
-> [[0 0 0] [0 0 1] [0 1 0] [0 1 1] [0 0 0 1] [0 0 0 1]] // [0 0 0]

[[0 0 1] [0 1 0] [0 1 1] [0 0 0 1] [0 0 0 1]] + [0 0 1 0] // [0 0 1]
-> [[0 0 1] [0 1 0] [0 1 1] [0 0 0 1] [0 0 0 1] [0 0 1 0]] // [0 0 1]
[[0 0 1] [0 1 0] [0 1 1] [0 0 0 1] [0 0 0 1] [0 0 1 1]] + [0 0 1 1] // [0 0 1]
-> [[0 0 1] [0 1 0] [0 1 1] [0 0 0 1] [0 0 0 1] [0 0 1 1] [0 0 1 1]] // [0 0 1]

all went fine till the fourth iteration of the loop :
the last “append” update the value appended just before

so, I have 2 questions (maybe with only one answer/explanation):

  • What causes this weird thing ?
  • Why does it do not always happen ?

hope I’ll find some answers :stuck_out_tongue:

I think what you are missing is that append does not always return a new slice. From the spec:

append(s S, x ...T) S // T is the element type of S

If the capacity of s is not large enough to fit the additional values, append allocates a new, sufficiently large underlying array that fits both the existing slice elements and the additional values. Otherwise, append re-uses the underlying array.

I think Go Playground - The Go Programming Language does what you intend.

is this why it happens only at the fourth iteration (related to the capping of my slice)? will it happens again at another iteration ?
is your solution 100% sure whatever the number of iteration ?

haha so many questions :stuck_out_tongue:

That is my theory. Play around with it and find out :smile_cat:

I think my solution will work for any number of iterations, but I have not tested beyond the number of iterations shown.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.