Tiny Golang embedding code example of analogy to StringList implementation
of reStructuredText.
Run Code on Go Playground
package main
import "fmt"
type ParentList struct {
data []int
parent *ParentList
}
func NewParentList(data []int, parent *ParentList) ParentList {
return ParentList{
data: data,
parent: parent,
}
}
func (p *ParentList) GetSlice(start, stop int) ParentList {
return NewParentList(p.data[start:stop], p)
}
type ChildList struct {
ParentList
parent *ChildList
}
func NewChildList(data []int, parent *ChildList) ChildList {
return ChildList{
ParentList: NewParentList(data, nil),
parent: parent,
}
}
func (c *ChildList) GetSlice(start, stop int) ChildList {
return NewChildList(c.data[start:stop], c)
}
func main() {
// Parent List
b := NewParentList([]int{0, 1, 2, 3, 4, 5, 6, 7}, nil)
bs := b.GetSlice(2, 5)
fmt.Println(bs)
fmt.Println(bs.parent)
// Child List
c := NewParentList([]int{7, 6, 5, 4, 3, 2, 1, 0}, nil)
cs := c.GetSlice(2, 5)
fmt.Println(cs)
fmt.Println(cs.parent)
}
References: