github.com/aminjam/goflat@v0.4.1-0.20160331105230-ec639fc0d5b3/runtime/main.gotempl (about)

     1  package main
     2  import (
     3      "bytes"
     4      "fmt"
     5      "io/ioutil"
     6      "os"
     7      "text/template"
     8      )
     9  func checkError(err error, detail string) {
    10    if err != nil {
    11      fmt.Printf("Fatal error %s: %s ", detail, err.Error())
    12        os.Exit(1)
    13    }
    14  }
    15  func main() {
    16    data, err := ioutil.ReadFile("{{.GoTemplate}}")
    17      checkError(err, "reading template file")
    18      pipes := NewPipes()
    19      {{if ne .CustomPipes ""}}
    20      pipes.Extend(CustomPipes())
    21      {{end}}
    22      tmpl, err := template.New("").Funcs(pipes.Map).Parse(string(data))
    23      checkError(err, "parsing template file")
    24      var result struct {
    25        {{if gt (len .GoInputs) 0}}
    26        {{range .GoInputs}}
    27        {{.StructName}} {{.StructName}}
    28        {{end}}
    29        {{end}}
    30      }
    31    {{if gt (len .GoInputs) 0}}
    32    {{range .GoInputs}}
    33    result.{{.StructName}} = New{{.StructName}}()
    34    {{end}}
    35    {{end}}
    36    var output bytes.Buffer
    37      err = tmpl.Execute(&output, result)
    38      checkError(err, "executing template output")
    39      fmt.Println(string(output.Bytes()))
    40  }