Skip to content

Wiki: V1 & V2 #20

@suntong

Description

@suntong

(Repost from https://github.com/go-easygen/easygen/wiki/V1-&-V2)

Version 1

Info

https://gopkg.in/easygen.v1

Sample code, V1

func exampleV1(templateFile string, config templateData) error {
	t, err := easygen.ParseFiles(false, templateFile)
	check(err)
	env := make(map[string]string)
	for _, e := range os.Environ() {
		sep := strings.Index(e, "=")
		env[e[0:sep]] = e[sep+1:]
	}
	config.ENV = env
	err = t.Execute(os.Stdout, config)
	return err
}

Version 2

https://gopkg.in/easygen.v2

Sample code, V2

See

easygen/example_test.go

Lines 12 to 29 in 8022ae3

func ExampleProcess() {
tmpl0 := easygen.NewTemplate().Customize()
tmpl := tmpl0.Funcs(easygen.FuncDefs()).Funcs(egVar.FuncDefs())
tmplFileName := "test/var0"
easygen.Process(tmpl, os.Stdout, tmplFileName)
// To use Execute(), TemplateFileName has to be exact
m := easygen.ReadDataFile(tmplFileName + ".yaml")
easygen.Execute(tmpl, os.Stdout, tmplFileName+".tmpl", m)
// Output:
// Input: "some-init-method"
// Output 1: "SomeInitMethod"
// Output 2: "SOME_INIT_METHOD"
// Input: "some-init-method"
// Output 1: "SomeInitMethod"
// Output 2: "SOME_INIT_METHOD"
}

In above code, to define tmpl with basic easygen functions without the addon ones, do

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions