title: Go slice element replaced by last element
body: <p>My problem is the answer should has [1 1 2 2], but it got [1 1 2 3].
  I debuged this code, found that in that loop, [1 1 2 2] was appended to the array firstly, but it changed to [1 1 2 3] in next loop.
  I don't understand why, the log which started with 'D', the elements in this array changed in next loop.
  Here is my code:
  <a href="https://play.golang.org/p/X_CU8GlMOqf" rel="nofollow noreferrer">https://play.golang.org/p/X_CU8GlMOqf</a></p>
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      &quot;fmt&quot;
  )
  
  func main() {
      nums := []int{1, 1, 2, 2, 3}
      result := subsetsWithDup(nums)
      fmt.Println(result)
  }
  
  func subsetsWithDup(nums []int) [][]int {
      result := [][]int{{}, nums}
      for i := 0; i &lt; len(nums); i++ {
          if i &gt; 0 &amp;&amp; nums[i] == nums[i-1] {
              continue
          }
          for j := 1; j &lt; len(nums); j++ {
              result = append(result, dsf(nums, []int{nums[i]}, i+1, j)...)
          }
      }
      return result
  }
  
  func dsf(nums []int, set []int, start int, length int) [][]int {
      result := [][]int{}
      if len(set) == length {
          return append(result, set)
      }
      for i := start; i &lt; len(nums); i++ {
          if i != start &amp;&amp; nums[i] == nums[i-1] {
              continue
          }
          if len(set) == 3 {
              fmt.Printf(&quot;A %v %p\n&quot;, result, &amp;result)
          }
          tmp := set[:]
          tmp = append(tmp, nums[i])
          if len(set) == 3 {
              fmt.Printf(&quot;B %v %p %v %p\n&quot;, tmp, &amp;tmp, result, &amp;result)
          }
          result = append(result, dsf(nums, tmp, i+1, length)...)
          if len(tmp) == 4 {
              fmt.Printf(&quot;C %v %p %v %p\n&quot;, tmp, &amp;tmp, result, &amp;result)
              for _, r := range result {
                  fmt.Printf(&quot;D %v %p\n&quot;, r, &amp;r)
              }
          }
      }
      return result
  }
  </code></pre>
  <pre class="lang-sh prettyprint-override"><code>A [] 0xc000004960
  B [1 1 2 2] 0xc0000049c0 [] 0xc000004960
  C [1 1 2 2] 0xc0000049c0 [[1 1 2 2]] 0xc000004960
  D [1 1 2 2] 0xc000004ae0
  A [[1 1 2 2]] 0xc000004960
  B [1 1 2 3] 0xc000004b60 [[1 1 2 3]] 0xc000004960
  C [1 1 2 3] 0xc000004b60 [[1 1 2 3] [1 1 2 3]] 0xc000004960
  D [1 1 2 3] 0xc000004ca0
  D [1 1 2 3] 0xc000004ca0
  A [] 0xc000004da0
  B [1 2 2 3] 0xc000004de0 [] 0xc000004da0
  C [1 2 2 3] 0xc000004de0 [[1 2 2 3]] 0xc000004da0
  D [1 2 2 3] 0xc000004f00
  </code></pre>