[Golang] Tiny Embedding Example of reStructuredText StringList Implementation


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:

[1]golang class inheritance
[2]Golang concepts from an OOP point of view - GitHub
[3]golang oop
[4]golang subclass
[5]golang inheritance override
[6]golang embedding
[7]golang interface inheritance
[8]golang method overloading
[9]Python docutils.statemachine.StringList Examples
[10]docutils.statemachine.StringList - Nullege Python Samples
[11]another tiny (bad) example on Go Playground
[12]Go Object Oriented Design (reddit)