See Docs:
A type may have a method set associated with it. The method set of an
interface type is its interface. The method set of any other type T
consists of all methods declared with receiver type T
. The method set
of the corresponding pointer type *T
is the set of all methods
declared with receiver *T
or T
(that is, it also contains the method
set of T
). Further rules apply to structs containing anonymous fields,
as described in the section on struct types. Any other type has an
empty method set. In a method set, each method must have a unique
non-blank method name.
And see: https://stackoverflow.com/a/33591156/6169399 :
If you have an interface I
, and some or all of the methods in I
's
method set are provided by methods with a receiver of *T
(with the
remainder being provided by methods with a receiver of T
), then *T
satisfies the interface I
, but T
doesn't. That is because *T
's method
set includes T
's, but not the other way around.
Using ctrl := Pages{}
makes error:
cannot use ctrl (type Pages) as type Handler in argument to Handle:
Pages does not implement Handler (Serve method has pointer receiver)
Using ctrl := Pages{}
needs:
func (p Pages) Serve() {
fmt.Println(p.i)
}
Iris Handler is an interface type. like this working sample (see comment):
package main
import "fmt"
type Handler interface {
Serve()
}
type Pages struct {
i int
}
func (p *Pages) Serve() {
fmt.Println(p.i)
}
func Handle(p Handler) {
p.Serve()
}
func main() {
// cannot use ctrl (type Pages) as type Handler in argument to Handle:
// Pages does not implement Handler (Serve method has pointer receiver)
//ctrl := Pages{}
ctrl := &Pages{101}
Handle(ctrl)
}
output:
101