I am writing a program to generate all possible permutations of a slice of unique elements. The code for the same can be found here
At one point I need to create a new slice from the original slice of numbers. This new slice has one less element than the original slice. I tried two different variants with append
nums
is the original slice and remaining
is the new slice I am trying to create.
i
is an int
which can range from 0
to len(nums)-1
.
Variant 1:
remaining := make([]int, 0)
remaining = append(remaining, nums[:i]...)
remaining = append(remaining, nums[i+1:]...)
Variant 2:
remaining := append(nums[:i], nums[i+1:]...)
While the program works fine with Variant 1 , it doesn't behave correctly with Variant 2. I want to understand what is the exact difference in these two Variants ?