This question already has an answer here:
- Golang slice reference confusion 3 answers
I'm attempting to implement Heap's Permutation Algorithm in Go. It's supposed to return all the possible permutations given an input set.
func Permute(in []string) [][]string {
c := make([]int, len(in))
out := make([][]string, 0)
for i := range out {
out[i] = make([]string, 0)
}
fmt.Println(in)
out = append(out, in)
i := 0
for i < len(in) {
if c[i] < i {
if i%2 == 0 {
in[0], in[i] = in[i], in[0]
} else {
in[c[i]], in[i] = in[i], in[c[i]]
}
fmt.Println(in)
out = append(out, in)
c[i]++
i = 0
} else {
c[i] = 0
i++
}
}
return out
}
The Println statements display the expected outputs. The out value is returned with the correct number of entries, but the entries are all the same. Please help me understand what I am missing.
</div>