I am very new to Go and so far I am loving it. However, I can't figure this out for the life of me.

I have a simple package, invoice.

type Invoice struct { key datastore.Key Name string Created time.Time Updated time.Time lineItems []LineItem }

An Invoice has several LineItems.

type LineItem struct { key datastore.Key InvoiceKey *datastore.Key Name string Description string }

My package has a couple functions.

func New(c appengine.Context) (i Invoice) func (i *Invoice) Update() func (i *Invoice) Save(c appengine.Context) func (i *Invoice) AddLineItem(c appengine.Context, name string)

New() returns a new Invoice, and saves the key generated by datastore.NewIncompleteKey() inside the Invoice as an unexported variable so that I can save it later.

All of that runs fine, whether it is the proper way to do this or not is another question. I'm open to comments on that too. What I can't seem to figure out is that last function.

func (i *Invoice) AddLineItem(c appengine.Context, name string) { key := datastore.NewIncompleteKey(c, "LineItem", &i.key) lineItem := LineItem{key: *key, InvoiceKey: &i.key, Name: name} i.lineItems = append(i.lineItems, lineItem) }

And in Save()

for _, lineItem := range i.lineItems { _, err := datastore.Put(c, &lineItem.key, &lineItem) if err != nil { panic(err) } }

I keep getting an invalid key error here. I'm basically just trying to make an Invoice have the ability to have many LineItems. Be able to save them all to the datastore, and then pull out the entire Invoice with all the LineItems as needed.

Am I on the right track? Is there a better way to do this?