package main import "fmt" type Ingredient struct { Name string Parts []Ingredient Location string Notes []string } type RecipeIngredient struct { Ingredient Quantity int } type Recipe struct { Name string Parts []RecipeIngredient Notes []string } func NewRecipe(nm string) *Recipe { ret := Recipe{Name: nm} return &ret } func (r *Recipe) AddNote(nt string) { r.Notes = append(r.Notes, nt) } func (r *Recipe) AddIngredient(nIng *Ingredient, qty int) { for i := range r.Parts { if r.Parts[i].Name == nIng.Name { // This ingredient is already a party of the recipe // Just increase the quantity r.Parts[i].Quantity += qty return } } ing := new(RecipeIngredient) ing.Name = nIng.Name ing.Parts = nIng.Parts ing.Location = nIng.Location ing.Quantity = qty ing.Notes = nIng.Notes r.Parts = append(r.Parts, *ing) } func (r *Recipe) FancyPrint() { fmt.Println("= "+r.Name+" =") for _, v := range r.Parts { fmt.Printf("* %d - %s\n", v.Quantity, v.Name) } }