This question already has an answer here:
- My object is not updated even if I use the pointer to a type to update it 3 answers
- Golang Operator Overloading 1 answer
- Golang changing values of a struct inside a method of another struct 2 answers
- Copy instances of type T, when any of the methods of a named type T have a pointer receiver 1 answer
I have a struct
type keeper struct {
ptr int32
}
then I add a function to it
func(l keeper) next() {
l.ptr++
}
But when I create a new keeper and call next()
tester := keeper {
ptr: 0,
}
test.next()
It seems I am not modifying the ptr value within tester. If I change the function to be a pointer it then works
func(l *keeper) next() {
l.ptr++
}
Why so?
</div>