github.com/jgarto/itcv@v0.0.0-20180826224514-4eea09c1aa0d/cmd/reactGen/core_gen.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 "text/template" 8 ) 9 10 type coreGen struct { 11 *gen 12 13 buf *bytes.Buffer 14 } 15 16 func newCoreGen(g *gen) *coreGen { 17 return &coreGen{ 18 gen: g, 19 buf: bytes.NewBuffer(nil), 20 } 21 } 22 23 func (c *coreGen) pf(format string, vals ...interface{}) { 24 fmt.Fprintf(c.buf, format, vals...) 25 } 26 27 func (c *coreGen) pln(vals ...interface{}) { 28 fmt.Fprintln(c.buf, vals...) 29 } 30 31 func (c *coreGen) pt(tmpl string, val interface{}) { 32 // on the basis most templates are for convenience define inline 33 // as raw string literals which start the ` on one line but then start 34 // the template on the next (for readability) we strip the first leading 35 // \n if one exists 36 tmpl = strings.TrimPrefix(tmpl, "\n") 37 38 t := template.New("tmp") 39 40 _, err := t.Parse(tmpl) 41 if err != nil { 42 fatalf("unable to parse template: %v", err) 43 } 44 45 err = t.Execute(c.buf, val) 46 if err != nil { 47 fatalf("cannot execute template: %v", err) 48 } 49 }