I am implementing a message passing system in Go. So I have a general interface called Msg
. The Msg
interface defines many common fields such as source, destination, send time, receive time, etc. I cannot define a full list of Msg
s since I want the library users to define the concrete type of Msg
s.
To provide a concrete type of Msg
, a user would need to implement a large list of getters and setters, which is very annoying.
One solution I tried is to provide a simple base class like MsgBase
and defines all the common properties and getters and setters. And for each concrete type of Msg
, I embed a pointer to the MsgBase
. This solution works.
But then, I want to embed a value version of the MsgBase
in the concrete Msg
types. This is because such Msg
s are created too many times in the execution and dynamically allocating a MsgBase
would increase the garbage collection overhead. I really want all the Msg
s are allocated statically since they are passed by the components and should never be shared. If I use the value version of the MsgBase
, I cannot use the setters defined in the MsgBase
.
I wonder if there is any simple solution to this problem?
EDIT: Adding sample code
type Msg interface {
// Agent is another interface
Src() Agent
SetSrc(a Agent)
Dst() Agent
SetDst(a Agent)
... // A large number of properties
}
type MsgBase struct {
src, dst Agent
... // Properties as private fields.
}
func (m MsgBase) Src() Agent {
return m.src
}
func (m *MsgBase) SetSrc(a Agent) {
m.src = a
}
... // Many other setters and getters for MsgBase
type SampleMsg struct {
MsgBase // option1
*MsgBase // option2
}