Go 无法将接口的实现用作需要接口的func的参数

Go 无法将接口的实现用作需要接口的func的参数,go,reflection,interface,Go,Reflection,Interface,我得到以下错误: ./main.go:31: cannot use telegramService (type messaging.TelegramService) as type mypackage.MessagingService in argument to mypackage.RegisterMessagingService: messaging.TelegramService does not implement mypackage.MessagingService (wron

我得到以下错误:

./main.go:31: cannot use telegramService (type messaging.TelegramService) as type mypackage.MessagingService in argument to mypackage.RegisterMessagingService:
    messaging.TelegramService does not implement mypackage.MessagingService (wrong type for HandleIncomingMessage method)
        have HandleIncomingMessage(telegram.Message) error
        want HandleIncomingMessage(mypackage.IncomingMessage) error
我有一个描述电报或WhatsApp等消息传递服务的接口,还有一个描述来自这些服务之一的传入消息的接口:

// IncomingMessage is a message that comes in on a messaging service
type IncomingMessage interface {
    Send() error
}

// MessagingService is a service on which messages can be send (like Telegram or FB Messenger)
type MessagingService interface {
    Start()
    HandleIncomingMessage(IncomingMessage) error
    GetHTTPHandler() http.HandlerFunc
    GetCommands() []MessagingCommand
}
MessagingService
的第一个实现是用于电报。问题在于
HandleIncomingMessage
函数,该函数目前实际上什么都不做,只是看起来像这样:

// HandleIncomingMessage will take an incoming message and repond to it
func (s TelegramService) HandleIncomingMessage(msg *telegram.Message) error {

    return nil
}
问题是此函数接受
电报.Message
,编译器说它不符合接口。问题是,
telegrame.Message
IncomingMessage
的一个实现:

// Message is a Telegram message
type Message struct {
    // Added the line below at some point, but it didn't work without it either
    mypackage.IncomingMessage
    MessageID uint64 `json:"message_id"`
    FirstName string `json:"first_name"`
    Username  string `json:"username"`
    Date      uint64 `json:"date"`
    Text      string `json:"text"`
    Chat      Chat   `json:"chat"`
    From      User   `json:"from"`
}

// Send will take m and send it
func (m Message) Send() error {
    // Do stuff

    return nil
}
最初,
IncomingMessage
是一个空界面,这是我第一次注意到这个问题的地方。我尝试添加函数
Send()
,反正我也要添加这个函数,因为我想给它任何结构都不起作用。然而,我仍然得到这个错误

我看不出为什么
telegram.Message
没有实现接口,它非常简单

有人能解释为什么这不起作用吗


PS:My package实际上没有被称为
mypackage
,为了清晰起见进行了更改

HandleIncomingMessage
必须采用
IncomingMessage
参数,因为这是接口的定义方式。您不能定义以其他类型作为参数的
HandleIncomingMessage
的实现,即使该类型实现了
IncomingMessage
。您可以定义函数以获取
IncomingMessage
,并使用类型断言将其转换为
*电报。Message

func (s TelegramService) HandleIncomingMessage(im IncomingMessage) error {
    msg := im.(*telegram.Message)
    return nil
}

我假设您实际上想要使用指向
电报.Message
的指针。如果是这样,您需要更改
Send
方法的定义以获取指针接收器。

方法签名为
HandleIncomingMessage(IncomingMessage)error
。您不能使用不同的签名,但为什么需要使用呢?编译器的投诉是有效的。电报服务不能处理任何收入信息,只能处理一些收入信息,因此不能满足MessagingService接口的要求。@JimB谢谢,先生。如果我使用IncomingMessage,我能不能把它当作电报一样使用?Message?@RoemerBakker:我不明白。如果希望它成为一个接口,那么可以通过其接口方法使用它。您总是可以将其断言为具体类型,但为什么首先要使用接口?@JimB此函数特定于电报。不过,我将沿着边逻辑实现其他消息传递服务,以将传入消息存储在数据库中。在这种情况下,我希望IncomingMessage是一个接口,这样我就可以为db存储编写一般的东西。尽管如此,这些传入消息也将具有特定的功能