title: Backtracking in Go to find all paths in Directed Acyclic Graph, problem assigning paths to a solution slice (Leetcode 797)
body: <p>I am attempting <a href="https://leetcode.com/problems/all-paths-from-source-to-target/" rel="nofollow noreferrer">Leetcode 747</a> in Go. The problem summary:</p>
  <blockquote>
  <p>Given a directed acyclic graph (DAG) of n nodes labeled from 0 to n -
  1, find all possible paths from node 0 to node n - 1, and return them
  in any order.</p>
  <p>The graph is given as follows: graph[i] is a list of all nodes you can
  visit from node i (i.e., there is a directed edge from node i to node
  graph[i][j]).</p>
  </blockquote>
  <p>This is my solution so far:</p>
  <pre class="lang-golang prettyprint-override"><code>func allPathsSourceTarget(graph [][]int) [][]int {
      allSolutions := [][]int{}
      target := len(graph) - 1
  
      isSolution := func(current int) bool {
          return current == target
      }
  
      processSolution := func(solution []int) {
          allSolutions = append(allSolutions, solution)
      }
  
      var backtrack func(currentPath []int)
  
      backtrack = func(a []int) {
          currentNode := a[len(a)-1]
          if isSolution(currentNode) {
              processSolution(a)
          } else {
              candidates := graph[currentNode]
              for _, c := range candidates {
                  a = append(a, c)
                  backtrack(a)
                  a = a[:len(a)-1]
              }
          }
      }
  
      backtrack([]int{0})
  
      return allSolutions
  }
  </code></pre>
  <p>It passes 7/30 inputs but then fails on this one <code>[[4,3,1],[3,2,4],[3],[4],[]]</code>. The expected output for which is <code>[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]</code>.</p>
  <p>I believe the problem is how I am appending each result to the <code>allSolutions</code> slice. If I log the solution that comes in each time, it's what is expected, but it seems to then mutate the solutions already added.</p>
  <p>If I add logs to the <code>allSolutions</code> func, for the above input, this is the output:</p>
  <pre><code>Solution:
  [0 4]
  New allSolutions:
  [[0 4]]
  Solution:
  [0 3 4]
  New allSolutions:
  [[0 3] [0 3 4]]
  Solution:
  [0 1 3 4]
  New allSolutions:
  [[0 1] [0 3 4] [0 1 3 4]]
  Solution:
  [0 1 2 3 4]
  New allSolutions:
  [[0 1] [0 3 4] [0 1 2 3] [0 1 2 3 4]]
  Solution:
  [0 1 4]
  New allSolutions:
  [[0 1] [0 3 4] [0 1 4 3] [0 1 2 3 4] [0 1 4]]
  </code></pre>
  <p>I am interested in knowing why this is happening. Is it related to modifying the variable from a higher scope?</p>