Skip to content

How to use easygen API

suntong edited this page Aug 5, 2020 · 5 revisions

Init

easygen's API has been the same since V2. I.e., use easygen.NewTemplate() to init a new template, with the built-in base template functions:

        tmpl := easygen.NewTemplate().Funcs(easygen.FuncDefs())

Execute

then use easygen.Execute():

	err := easygen.Execute(tmpl, os.Stdout, templateFile, settingVar)

To variable

Or, to get the result into variable, use the following:

        buf := new(bytes.Buffer)
        err := easygen.Execute(tmpl, buf, templateFile, settingVar)
        check(err)
        return buf.String()

Using template string

To directly use template string instead of template file, use the following:

        buf := new(bytes.Buffer)
        err := easygen.Process0(tmpl, buf, templateStr, yamlFileName)

Execute0, zero template file and data file

To directly use template string on variable (without an input data file), use the following:

        var b strings.Builder
        err := easygen.Execute0(tmpl, &b, templateStr, dataOfAnyType)

See the Example under Execute0() godoc for full code.

Finally, to use easygen.NewTemplate() to init a new template, with all the easygen provided template functions:

        tmpl0 := easygen.NewTemplate().Customize()
        tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs()).Funcs(egCal.FuncDefs()).Funcs(egFilePath.FuncDefs())

See the Example under Execute() godoc for full code.

See also,

Clone this wiki locally