Go version

go1.23.9.windows-amd64

Output of go env in your module/workspace:

GOARCH=amd64

What did you do?

func combinationSum(candidates []int, target int) [][]int {
    sort.Ints(candidates)
    var result [][]int
    var back_func func(candidates []int, target int, tmp []int, index int)
    back_func = func(candidates []int, t int, tmp []int, index int) {
        if t <= 0 {
            if t == 0 {
                fmt.Println("tmp:", tmp, "t:", t, " ", index, " addr,", &tmp)
                result = append(result, tmp)
                fmt.Println("result:", result)
            }
            return
        }

        for i := index; i < len(candidates); i++ {
            if len(result) > 150 {
                return
            }

            //只往后走就ok
            candidate := candidates[i]
            nt := t - candidate

            if nt >= 0 {
                ntmp := append(tmp, candidate)
                //fmt.Println("candidate", candidate, ntmp, i)
                back_func(candidates, nt, ntmp, i)
            } else {
                //fmt.Println("candidate", candidate, i, nt, tmp)
                return
            }
        }
    }

    back_func(candidates, target, []int{}, 0)
    return result
}
func main() {
    fmt.Println(combinationSum([]int{7, 3, 2}, 18))
}

output is: tmp: [2 2 2 2 2 2 2 2 2] t: 0 0 addr, &[2 2 2 2 2 2 2 2 2]
result: [[2 2 2 2 2 2 2 2 2]] //here result is ok tmp: [2 2 2 2 2 2 3 3] t: 0 1 addr, &[2 2 2 2 2 2 3 3] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3]] //here result is ok tmp: [2 2 2 2 3 7] t: 0 2 addr, &[2 2 2 2 3 7] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7]] //but why here result[1] become [2 2 2 2 2 7 3 3]? tmp: [2 2 2 3 3 3 3] t: 0 1 addr, &[2 2 2 3 3 3 3] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3]] tmp: [2 2 7 7] t: 0 2 addr, &[2 2 7 7] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7]] tmp: [2 3 3 3 7] t: 0 2 addr, &[2 3 3 3 7] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7] [2 3 3 3 7]] tmp: [3 3 3 3 3 3] t: 0 1 addr, &[3 3 3 3 3 3] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7] [2 2 2 3 3 3 3] [2 2 7 7] [2 3 3 3 7] [3 3 3 3 3 3]]

What did you see happen?

output is: tmp: [2 2 2 2 2 2 2 2 2] t: 0 0 addr, &[2 2 2 2 2 2 2 2 2]
result: [[2 2 2 2 2 2 2 2 2]] //here result is ok tmp: [2 2 2 2 2 2 3 3] t: 0 1 addr, &[2 2 2 2 2 2 3 3] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3]] //here result is ok tmp: [2 2 2 2 3 7] t: 0 2 addr, &[2 2 2 2 3 7] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 7 3 3] [2 2 2 2 3 7]] //but why here result[1] become [2 2 2 2 2 7 3 3]? I never change result value

What did you expect to see?

The output of the third result should be: tmp: [2 2 2 2 2 2 2 2 2] t: 0 0 addr, &[2 2 2 2 2 2 2 2 2] result: [[2 2 2 2 2 2 2 2 2]] //here result is ok tmp: [2 2 2 2 2 2 3 3] t: 0 1 addr, &[2 2 2 2 2 2 3 3] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3]] //here result is ok tmp: [2 2 2 2 3 7] t: 0 2 addr, &[2 2 2 2 3 7] result: [[2 2 2 2 2 2 2 2 2] [2 2 2 2 2 2 3 3] [2 2 2 2 3 7]] //this is ok

Comment From: randall77

This is not a bug.

Append does not always allocate a new backing store. You might get some insight from https://go.dev/blog/slices-intro TL;DR always assign the results of append back to the first argument.