In golang, I'm having the following compile error:
cannot use m (type MessageImpl) as type Message in assignment:
MessageImpl does not implement Message (missing FirstLine method)
Using the following code:
type Message interface {
FirstLine() string
/* some other method declarations*/
}
type MessageImpl struct {
headers []Header
/* some other fields */
}
type Request struct {
MessageImpl
/* some other fields */
}
type Response struct {
MessageImpl
/* some other fields */
}
func (r Request) FirstLine() string {
var requestLine bytes.Buffer
requestLine.WriteString("...")
// Some more instructions
return requestLine.String()
}
func (m MessageImpl) RenderMessage() string {
var message bytes.Buffer
var msg Message = m // <=== This is the line the compiler moans about
message.WriteString(msg.FirstLine())
// Some more instructions
return message.String()
}
Coming from java language, I'll try to express my intention:
Having some interface Message
defining some methods implemented by an abstract class MessageImpl
. Two real classes Request
and Response
should inherit from MessageImpl
(and implement the interface Message
). Both of these define and implement some more internal methods.
How is this done best?