I am reading the docs for the sort stdlib package and the sample code reads like this:
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
As I've learnt, function that mutate a type T
needs to use *T
as its method receiver.
In the case of Len
, Swap
and Less
why does it work ? Or am I misunderstanding the difference between using T
vs *T
as method receivers ?