Hello,
Borja Roux Lorenzo <
borja...@gmail.com>:
>According to the definition of sort.Interface<
http://golang.org/pkg/sort/#Interface>,
>interface operations must be defined without a pointer.
>In documentation of package "sort", I find example "*Example (SortKeys)*".
>
>In this example, type "planetSorter" is defined and the functions that
>implement the interface use pointers in their definition: "func (s
>*planetSorter) Len() int".
This just means that not planetSorter, but *planetSorter implements the
interface.
>Why does type "planetSorter" in "Example (SortKeys)" in documentation of
>"sort" package work? Shouldn't we receive a compilation error?
In this example, sort.Sort(ps) is invoked, where ps is of type
*planetSorter.
The example would also work if no pointer receivers were used, and
ps was of type planetSorter.
Moreover, it works without pointer receivers even if ps has type
*planetSorter, because the method set of a pointer type includes
the method set of the type it points to. (See
*
http://golang.org/ref/spec#Method_sets )
Regarding this, the example might be considered unneccesarily complex.
Harald