Currently, I'm having two files in two packages.
File somepkg/something.go
package somepkg
// ----------------------------------------------------------------------------
// Interfaces
// ----------------------------------------------------------------------------
type SomeThing interface {
// some other methods
Handle(handler *Handler)
}
type SomeThingImpl struct {
handlers []Handler
}
type Handler interface {
IncomingCall(request *IncomingRequest)
}
// ----------------------------------------------------------------------------
// SomeThingImpl implementation
// ----------------------------------------------------------------------------
func NewSomeThing() SomeThing {
u := new(SomethingImpl)
// some more operations with u
return u
}
func (l *SomeThingImpl) Handle(handler *Handler) {
fmt.Printf("handler: %s", handler)
}
File main.go
package main
import (
"fmt"
)
type MyHandler struct {}
func (h *MyHandler) IncomingCall(request *somepkg.IncomingRequest) {
fmt.Printf("Handler!")
}
func main() {
mySomeThing := somepkg.NewSomeThing()
handler := new(MyHandler)
// works so far.
mySomeThing.Handle(handler) // <-- here the compilation error occurs
}
trying to run go build
yields the following compilation error:
{...}\main.go:20: cannot use handler (type *MyHandler) as type *somepkg.Handler in argument to mySomething.Handle:
*somepkg.Handler is pointer to interface, not interface
whereas main.go:20
refers to the line above where I'm calling mySomeThing.Handle(handler)
.
Actually, both MyHandler
and somepkg.Handler
seem to be pointers. Both of them implement the same methods.
Why does the compiler not consider these types to be compatible ?