Appending an integer slice to a slice of integer slices modifies the slice that gets appended

I’m trying to append a slice of integers to a slice that is made up of integer slices. When I print the slice, it shows up as expected. However, when I append the slice to a slice of slices, the content changes.

package main

import "fmt"

var myGraph [8][8]bool //the graph

var visited [8]bool //an array that marks if visited

var path []int //a slice to store a possible path

var paths [][]int

func dfs(src int, dest int) {
    //add current node to path
    path = append(path, src)

    //mark current node as visited
    visited[src] = true

    //if the current node is the destination
    //print the path and return
    if src == dest {
        fmt.Println(path)
        paths = append(paths, path) //I'm trying to push the path slice into the paths slice
        return
    }

    for i := 1; i <= 7; i++ { //loop through all nodes

        //if ith node is a neighbour of the current node and it is not visited
        if myGraph[src][i] && visited[i] == false {

            // call dfs on the current node
            dfs(i, dest)

            //mark the current node as unvisited
            //so that we can other paths to the final destination
            visited[i] = false

            //re-slice the slice - get rid of the current node
            path = path[:len(path)-1]
        }

    }

}

func main() {
    path = make([]int, 0, 8)
    paths = make([][]int, 0, 10)

    //creating the graph
    myGraph[1] = [...]bool{false, false, true, true, false, false, true, false}
    myGraph[2] = [...]bool{false, true, false, true, false, true, false, false}
    myGraph[3] = [...]bool{false, true, true, false, true, false, true, false}
    myGraph[4] = [...]bool{false, false, false, true, false, false, true, false}
    myGraph[5] = [...]bool{false, false, true, false, false, false, true, false}
    myGraph[6] = [...]bool{false, true, false, true, true, false, false, true}
    myGraph[7] = [...]bool{false, false, false, false, false, false, true, false}

    //call dfs by feeding in the source and the destination
    dfs(1, 7)
    fmt.Println(paths)
}

Output:

[1 2 5 6 7]
[1 3 2 5 6 7]
[1 3 4 6 7]
[1 3 6 7]
[1 6 7]
[[1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3 2] [1 6 7 3 2 5] [1 6 7 3 2] [1 6 7 3] [1 6 7]]

As you can see, the slice of slices is printed at the end by the main function. However, its contents are different to the individual slices that are printed by dfs().

Append may return either a slice that points to the same backing array or a slice pointing to a newly allocated and copied array. As you’re calling dfs recursively and appending to the same global slice copies may happen at some levels and not at others. You’ll get inconsistencies as an existing slice can get modified by future calls to dfs.

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