Your code encounters a variable overwriting issue because you are creating new slices (pathA and pathB) by appending elements to the same backing array (route) within a loop.
Background on Slices in Go:
Problem with Your Code:
In your code, you are creating two new slices, pathA and pathB, using the append function:
pathA := append(route, nextA) pathB := append(route, nextB)
Here's what happens:
Solution:
To avoid this overwriting, you need to ensure that pathA and pathB have unique backing arrays. You can achieve this by manually creating a new slice for one of them using make and copy:
newRoute := make([]int, len(route), (cap(route) 1)*2) copy(newRoute, route) if i % 2 == 0 { pathA := append(newRoute, nextA) } else { pathB := append(newRoute, nextB) }
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3