나는 다음과 같은 오류지고있어 원하는 FUNC에 인수로 인터페이스의 구현을 사용할 수 없습니다 :인터페이스
./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
}
문제는이 함수는 컴파일러가 인터페이스를 준수하지 않는라는 telegram.Message
을 받아들이는 것이다.
// 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
내가 먼저 문제를 발견 어디 빈 인터페이스이었다 것은 telegram.Message
는 구현 IncomingMessage
의이라고한다. 나는 어쨌든 추가 할 함수 인 Send()
을 추가하려고 시도했다. 어쩌면 단지 구조체를 쓰지 않을 것이라고 생각했기 때문이다. 그러나 여전히이 오류가 발생합니다.
telegram.Message
이 인터페이스를 구현하지 못하는 이유가 표시되지 않습니다. 매우 간단합니다.
왜 이것이 작동하지 않는지 설명 할 수 있습니까?
PS : 내 패키지가 실제로는 인터페이스가 정의 된 방법 년부터 IncomingMessage
인수를 취해야합니다 선명도
메소드 서명은'HandleIncomingMessage (IncomingMessage) error'입니다. 다른 서명을 사용할 수는 없지만 왜 그렇게해야합니까? – JimB
컴파일러의 이의가 유효합니다. TelegramService는 * IncomingMessage를 처리 할 수 없으므로 MessagingService 인터페이스를 만족하지 못합니다. – Peter
@ 짐바 감사합니다. IncomingMessage를 사용하면 텔레 그램처럼 사용할 수 있습니까? 메시지. –