Suppose I wanted to write some code that would let me represent functions as trees. For example, the functions -2 and x+y would be represented as these trees:

In Java, I'd write a top-level class that looks something like this:

```
public abstract class Node {
private Node parent;
private Node[] children;
public abstract Object evaluate();
}
```

Then I could define the various nodes, such as:

```
public class Negate extends Node {
public Object evaluate() {
return -((Double) children[0].evaluate());
}
}
```

public class Two extends Node {
public Object evaluate() {
return new Double(2.0);
}
}

public class Add extends Node {
public Object evaluate() {
return ((Double) children[0].evaluate()) +
((Double) children[1].evaluate());
}
}

In Go, however, we don't have class hierarchies or abstract methods. Consider this first attempt:

```
type Node struct {
Parent *Node
Children []*Node
}
```

func (*Node) Evaluate() interface{} { return nil }

type Negate struct {
*Node
}

func (n *Negate) Evaluate() interface{} {
return -n.Children[0].Evaluate().(float64)
}

type Two struct {
*Node
}

func (*Two) Evaluate() interface{} {
return 2.0
}

What I've done here is to embed a ***Node** inside a **Negate**. This is *composition* and not *inheritance*. It is really just a convenience that if **Negate** does not define a field or func, then if one of **Negate**'s embedded structs defines it, that is what's used.

For example, the reference to the **Children** field in **Negate**'s **Evaluate** func is a reference to the embedded ***Node**'s **Children** field.

But because **Children** in a ***Node** is a **[]*Node**, that means that **Children[0] **is a ***Node**, and thus **Children[0].Evaluate()** is a call to a func on ***Node**, not to anything else.

For example:

```
neg := &Negate{&Node{Children: make([]*Node, 1)}}
two := &Two{&Node{}}
neg.Children[0] = two
cannot use two (type *Two) as type *Node in assignment
```

This fails because two is not a ***Node**, it is a ***Two**. We can try to correct this:

```
neg.Children[0] = two.Node
neg.Evaluate()
panic: interface conversion: interface is nil, not float64
```

The panic is correct: within **Negate**'s **Evaluate** func, **Children[0].Evaluate()** is a call to ***Node**'s **Evaluate** func, which returns **nil**.

Conceptually, the pointer diagram looks like this:

The diagram makes it clear that **Children** always point to a **Node**. This is a direct consequence of embedding not being inheritance.

The basic problem is that when we call Evaluate, we need to call it *on the original struct*, not on the embedded struct. We can do this by using an interface:

```
type Evaluable interface {
Evaluate() interface{}
}
type Node struct {
Parent Evaluable
Children []Evaluable
}
type Negate struct {
*Node
}
func (n *Negate) Evaluate() interface{} {
return -n.Children[0].Evaluate().(float64)
}
type Two struct {
*Node
}
func (n *Two) Evaluate() interface{} {
return 2.0
}
func main() {
neg := &Negate{&Node{Children: make([]Evaluable, 1)}}
two := &Two{&Node{}}
neg.Children[0] = two
fmt.Printf("%v", neg.Evaluate().(float64))
}
-2
```

So far, so good. Now, let's start adding common funcs to Node, such as one which tells you which child of a parent you are:

```
package main
import (
"errors"
"fmt"
)
type Evaluable interface {
Evaluate() interface{}
GetChildren() []Evaluable
}
type Node struct {
Parent Evaluable
Children []Evaluable
}
func (n *Node) Evaluate() interface{} { return nil }
func (n *Node) GetChildren() []Evaluable { return n.Children }
func (child *Node) WhichChild() (int, error) {
for i, c := range child.Parent.GetChildren() {
if c == child {
return i, nil
}
}
return 0, errors.New("Not found")
}
type Negate struct {
*Node
}
func (n *Negate) Evaluate() interface{} {
return -n.Children[0].Evaluate().(float64)
}
type Two struct {
*Node
}
func (n *Two) Evaluate() interface{} {
return 2.0
}
func main() {
neg := &Negate{&Node{Children: make([]Evaluable, 1)}}
two := &Two{&Node{}}
neg.Children[0] = two
two.Parent = neg
c, ok := two.WhichChild()
fmt.Printf("%v %v\n", c, ok)
fmt.Printf("%p %q\n", two, two)
fmt.Printf("%p %q\n", neg, neg)
}
0 Not found
0x1040a130 &{%!q(*main.Node=&{0x1040a128 []})}
0x1040a128 &{%!q(*main.Node=&{<nil> [0x1040a130]})}
```

I had to do a few things here. First, in order for us to get the **Children** of a **Node**'s **Parent**, I had to give **Evaluable** a func that gets the **Children**.

Then I had to implement **GetChildren** and **Evaluate** in **Node** to make **Node** an **Evaluable**, so that inside **WhichChild** I could compare **Evaluable** to **Evaluable**.

But it doesn't seem to work! The pointers that I print at the end show that **neg.Children[0]** is equal to **two**, so what's going on?

Notice that we call **two.WhichChild**. But the receiver of **WhichChild** isn't a ***Two**, it's a ***Node**. Thus, we're actually comparing **two.Node** to **two**, which will never work. So what can we do?

We can fix this easily by making WhichChild a receiverless function, and giving it an Evaluable as an argument.