I'm writing a document generator. There is a DocumentItem
interface - this is part of the document.
type DocumentItem interface {
compose() string
}
For example, a document consists of paragraphs and tables.
type Paragraph struct {
text string
}
type Table struct{}
The Paragraph
and Table
types correspond to the DocumentItem
interface.
func (p *Paragraph) compose() string {
return ""
}
func (t *Table) compose() string {
return ""
}
The Document
type contains the content []*DocumentItem
field.
type Document struct {
content []*DocumentItem
}
I'm looking for a way that would allow NewParagraph()
and NewTable()
functions to create the necessary data types and add them to the content
field.
func (d *Document) NewParagraph() *Paragraph {
p := Paragraph{}
d.content = append(d.content, &p)
return &p
}
func (d *Document) NewTable() *Table {
t := Table{}
d.content = append(d.content, &t)
return &t
}
I use an slice of interface pointers in order to be able to modify the data in the corresponding variables after they are included in the document.
func (p *Paragraph) SetText(text string) {
p.text = text
}
func main() {
d := Document{}
p := d.NewParagraph()
p.SetText("lalala")
t := d.NewTable()
// ...
}
But I get compiler errors:
cannot use &p (type *Paragraph) as type *DocumentItem in append
cannot use &t (type *Table) as type *DocumentItem in append
If I cast the types to an DocumentItem
interface, I will lose access to specific functions, which in the case of one type may differ from the other. For example, add text to a paragraph and add a row of cells and then add text to cell for the table.
Is it possible at all?
Full example at https://play.golang.org/p/uJfKs5tJ98