package main import ( "fmt" "io" ) // new idea. useful? TODO: look at this again in 1y func (msg *MsgName) insertBy(w io.Writer, FRUIT, APPLES, APPLE string, COLOR string) { LOCK := msg.getLockname("x") fmt.Fprintln(w, "// returns an "+APPLE+" if "+COLOR+" matches, otherwise create") fmt.Fprintln(w, "func (x *"+FRUIT+") InsertBy"+COLOR+" (y string) *"+APPLE+" {") fmt.Fprintln(w, " "+LOCK+".Lock()") fmt.Fprintln(w, " defer "+LOCK+".Unlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " for _, p := range x."+APPLES+" {") fmt.Fprintln(w, " if p."+COLOR+" == y {") fmt.Fprintln(w, " return p") fmt.Fprintln(w, " }") fmt.Fprintln(w, " }") fmt.Fprintln(w, "") fmt.Fprintln(w, " z := new("+APPLE+")") fmt.Fprintln(w, " z."+COLOR+" = y") fmt.Fprintln(w, " x."+APPLES+" = append(x."+APPLES+", z)") fmt.Fprintln(w, " return z") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") }