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