1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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, "")
}
|