A stupid-simple template substitution tool.
This code supports the following three features:
${var}
— a template variable which will be replaced with the value ofvar
\$
— an escape, which will be replaced with a$
\\
— an escape, which will be replaced with a\
No other special syntax is supported.
By default, the gotmpl
cli tool will resolve template variables from the current environment. As an argument, it takes a file to template and prints the result to stdout.
For example:
$ cat input_file
hello ${VAR}
$ export VAR=world
$ gotmpl input_file
hello world
$ gotmpl input_file > output_file
$ cat output_file
hello world
A template may be evaluated by providing anything which satisfies the Lookup
interface. The most trivial thing to use is a go map via the MapLookup
:
result, err := gotmpl.TemplateString("foo is ${foo}", gotmpl.MapLookup(map[string]string{"foo": "value"}))
fmt.Println(result)
// Prints: foo is value