Situation:
I've learned about pointer receivers and value receivers. From what I understand: if you want to modify the object itself, you need to use a pointer receiver. I was reading more about interfaces in the go documentation and found this chunk of code:
type Sequence []int
// Methods required by sort.Interface.
func (s Sequence) Len() int {
return len(s)
}
func (s Sequence) Less(i, j int) bool {
return s[i] < s[j]
}
func (s Sequence) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
The
Less
andLen
methods are using value receivers and this makes sense, because they are returning data and not modifying theSequence
state.But in the
Swap
example, I am curious why it is still using a value receiver when it looks like it is trying to modify its state.
Question:
Is this a mistake, or is my understanding of value/pointer receivers flawed in some way?