On the code I'm working on I'm experiencing this behavior where I mutate the element of a Struct
and the element seems changed but the value of the element changed, in the struct, is not!
I'm obviously missing something as I assumed that iterating over the element of the structure mutating it, the structure will reflect this as well. Is Go copying the element of the structure and passing it's value to my function, which in turn mutates the copy and not the original, right?
Worst thing is that I have to run the function that mutates the element of the structure concurrently, I'm afraid that what I want to achieve is possible but only it wouldn't be "thread safe".
If you down vote this post, please explain at least why. Thank you!
评论:
binaryblade:
shackra:because the local in the for loop is a copy of the object in your slice of slices. When you modify the copy it does not modify the object in your slice of slices
edit: here
bscharm:Silly me, thank you!
shackra:when performing range you are working with a copy of the values from your struct Prueba. you can see this by logging the address of n versus the address of p.Lista[i] where i is the current index in your iteration.
tgulacsi:ah! my suspicion was confirmed then, thanks!
See https://play.golang.org/p/K6o7gT5YfB What do you write concurrently?
