I am studying Iris framework recently. I encountered a question when I was implementing the Handler. Like following:
package controller
import "github.com/kataras/iris"
type Pages struct{
}
func (p *Pages) Serve(c *iris.Context) {
}
In order to use this controller, I implemented the following entry script:
package main
import (
"github.com/kataras/iris"
"web/controller"
)
func main(){
ctrl := controller.Pages{}
iris.Handle("GET", "/", ctrl)
iris.Listen(":8080")
}
But when I compiled the code, I got the following error message:
cannot use ctrl (type controllers.Pages) as type iris.Handler in argument to iris.Handle:
controllers.Pages does not implement iris.Handler (Serve method has pointer receiver)
After I changed the declaration to:
ctrl := &controller.Pages{}
Then the compiler passed with no complaint.
The question is: I thought the following statements are equal, since the GO compiler will do the conversion under the table:
type Pages struct {
}
func (p *Pages) goWithPointer() {
fmt.Println("goWithPointer")
}
func (p Pages) goWithValue() {
fmt.Println("goWithValue")
}
func main() {
p1 := Pages{}
p2 := &Pages{}
p1.goWithPointer()
p1.goWithValue()
p2.goWithPointer()
p2.goWithValue()
}
Why can't I use ctrl := controller.Pages{}
as the parameter to iris.Handle()
, instead of ctrl := &controller.Pages{}
as the parameter to iris.Handle()
?
Thank you for your time and sharing.
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 typeT
. The method set of the corresponding pointer type*T
is the set of all methods declared with receiver*T
orT
(that is, it also contains the method set ofT
). 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 inI
's method set are provided by methods with a receiver of*T
(with the remainder being provided by methods with a receiver ofT
), then*T
satisfies the interfaceI
, butT
doesn't. That is because*T
's method set includesT
'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
According to https://godoc.org/github.com/kataras/iris#Handler. Iris Handler is an interface type.
GO performs implicit pointer-conversion on variables only, not on interfaces.