How can this happen recursive function

How can this happen? What should I look for?

I have a recursive function play(b) that passes a struct as a parameter known internally as bIn.

bNew has two slices s and w. The slices are of a small struct c which has string1, string2 and bool as its components.

play(bIn) has code in it:

bNew := mm(bIn)
bOrigIn := bIn
play(bNew)

This is the only call to play in the function.

OK enough setup here is the issue: at some point say 23 levels into recursion play is called and continues down another 10 levels. Then returns back up to level 23. At that point bIn is different from before it was called. Curiously bOrigIn is also different from before with the exact same changes as seen in bIn. bNew is as it was before the call.

My question is how can this happen? All 3 variables are function level scope NOT package level.

More information about the changes to bIn: slices s and w are of both of len 9. bIn’s slice w returns with a different 9th element than when it left?

Now I know that at some point between level 23 and 33 the ninth element of w is changed to equal the ninth element of s with the boolean flipped and the 9th element of s removed. The change is made by a func called mm(bIn)

Specifically the value of bIn on return to level 23 has s as in the original but w is as described in paragraph above ie w[8]=s[8] with the boolean flipped.

I suspect it has something to do with slices of slices actually pointing to the same memory location as the original. That seems the only explanation for why bIn and bOrigIn have the same changes. But how can bIn be any different than before the call?

Thank you for reading all of this.

Is there something special I need to look for? Do I not understand what happens when I code bOrigIn:=bIn ? I have tried both := and just = having created bOrigIn first.

I don’t know what you’re talking about, can you write a demo code? Looking at the code is more clear about what the problem is than the text.

You are correct - rethought it and rewrote my quesion with sample code. See Go - I thought Go was a Call by Value language

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