dragon7713 2015-07-14 16:13
浏览 17
已采纳

无法在函数调用中使用某些类型

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 ?

  • 写回答

2条回答 默认 最新

  • douyan8413 2015-07-14 16:22
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭