Let's say we have such a structure (one of the simplest):
type some struct{
I uint32
}
And we want to have a variable of this type and atomically increase the cycle (maybe in another version of goroutine, but now the story is different). I do the following:
q := some{0}
for i := 0; i < 10; i++ {
atomic.AddUint32(&q.I,1)
fmt.Println(q.I)
}
We get what we expect so well, but if we declare a function for this type as follows:
func (sm some) Add1(){
atomic.AddUint32(&sm.I,1)
}
and call this function in the above example (line [1]), the value does not increase and we get only zeros. The question is obvious - why?
This should be something basic, but since I'm new, I don't get it.
source
share