Appending an integer slice to a slice of integer slices modifies the slice that gets appended
我试图将一个整数片附加到一个由整数片组成的片上。当我打印切片时,它会按预期显示出来。但是,当我将切片附加到切片时,内容会更改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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]] |
如您所见,切片的末尾由主函数打印。但是,它的内容不同于由df()打印的单个切片。
让我们稍微改变一下这个代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 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 { // (A) buffer := make([]int, len(path)) copy(buffer, path) fmt.Println(buffer) paths = append(paths, buffer) //I'm trying to push the path slice into the paths slice // 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 main1() { // 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) } |
现在它按预期工作!为什么?因为切片是引用类型,而不是值类型;意味着当您稍后更改