简体   繁体   中英

Cannot use some type in function call

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 ?

You have this method;

func (l *SomeThingImpl) Handle(handler *Handler) {
  fmt.Printf("handler: %s", handler)
}

Defined to take an interface pointer which is likely not what you want. What you're actually looking for is to have *MyHandler implement the Handler interface rather than MyHandler (or both could) so that you can pass your reference type into the method.

//new method sig
Handle(handler Handler)
// my preferred syntax for assignment here
handler := &MyHandler{}
// works just fine
mySomeThing.Handle(handler)

If you really want to keep the method sig how you have it, to make your code work you just need to do mySomeThing.Handle(&handler) but I'm doubtful that's actually what you want to do.

btw; In your code *MyHandler is already implementing the interface due to this method func (h *MyHandler) IncomingCall(request *somepkg.IncomingRequest) haveing *MyHandler as the receiving type as apposed to func (h MyHandler) which would make it so only MyHandler implements the Handler interface. Some little Go nuance with recievers and interfaces there.

Never use a pointer to an interface (well, unless you know exactly why you would want to do so)

In order to pass that into the defined function, you would need to create the *somepkg.Handler explicitly (but don't really do this)

var handler somepkg.Handler = new(MyHandler)
mySomeThing.Handle(&handler)

If you define Handle as

Handle(handler Handler)

any type that satisfies the somepkg.Handler interface can be passed in directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM