Answer the question
In order to leave comments, you need to log in
Why is this happening and how to do it right?
I'm trying to figure it out. Faced with strange behavior: https://play.golang.org/p/7NXR0BeHlgj
It turns out that "i" is always by reference, and when we assign "d", then the next time "i" changes, "d" will always change as well? What is the correct way to select in this case? Break put after the assignment is the only thing that came up with -_- . But such a strange "internal" feeling that I'm doing it wrong
Answer the question
In order to leave comments, you need to log in
here is the fix
https://play.golang.org/p/TR38meeQAH7
or
https://play.golang.org/p/WdGTLqGaoAD
this happens because this is how range works, it copies the element from the array and overwrites the i variable on each iteration , so you need to either copy it (as in example 1), or pass it through a closure (as in 2 and in the case of goroutines).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question