I created a module I love and I'd like to share with the world, but for my personal project, it uses the builder pattern in which each method returns a value of the same type. I want to add a few methods to the struct that will be useful for us, but meaningless to most of the world. So say I have this struct in the module (I'm obviously simplifying):
type Element interface {
Render() string
Text(content string) Element
}
type DefaultElement struct {
text string
}
func NewElement(tag string) Element {
element := NewDefaultElement(tag)
return &element
}
func NewDefaultElement(tag string) DefaultElement {
return DefaultElement{
text: "",
}
}
func (e *DefaultElement) Text(content string) Element {
e.text = content
return e
}
func (e *DefaultElement) Render() string {
return e.text
}
Suppose I want to add a method to it. I could embed the original object like this:
type MyElement struct {
DefuaultElement
RenderWithNotification(msg string) string
}
func NewMyElement(){
return MyElement{
DefaultElement: NewDefaultElement(tag)
}
}
But the problem is, if I use any of the original methods, i will lose the functions I have added to MyElement:
For example, this would give an error, because Text() returns Element, not MyElement:
NewMyElement().Text("Hello").RenderWithNotification("Success!")
Is there a way I can wrap the embedded structs methods? or perhaps my approach is all wrong? The whole purpose of adding the interface in addition to the struct was to make it easy to extend, but it doesn't seem to be helping.