Skip to content

Instantly share code, notes, and snippets.

@alecthomas
Created March 28, 2012 13:13
Show Gist options
  • Save alecthomas/2226031 to your computer and use it in GitHub Desktop.
Save alecthomas/2226031 to your computer and use it in GitHub Desktop.
type TaskNode struct {
children []*Node
}
func (self *Task) AddChild(node *TaskNode) {
self.children = append(self.children, node)
}
func (self *Task) ChildAt(index int) *TaskNode {
return self.children[index]
}
// There are other functions on *Task, omitted for brevity
// TaskList should behave like a TaskNode, but with extra functionality
type TaskList struct {
*TaskNode
title string
}
func (self *TaskList) SetTitle(title string) {
self.title = title
}
type Task struct {
*TaskNode
priority int
}
func (self *Task) GetPriority() int {
return self.priority
}
// So now the following should work...
list := NewTaskList()
list.SetTitle("a list")
task := NewTask()
task.SetPriority(10)
list.AddChild(task)
// How can I cast this to *Task?
taskRef := list.ChildAt(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment