Templates
Templates are useful for generating data-driven textual output. The text/template
package can be used in the following contexts:
- Configuration files: You can accept templates in configuration files, such as the following example that uses an
env
map variable to create environment-sensitive configurationslogfile: {{.env.logDir}}/log.json
- Reporting: Use templates to generate output for command-line applications and reports
- Web applications: The
html/template
package provides HTML-safe templating functionality for template-based HTML generation to build web applications
Value substitution
The main use of templates is inserting data elements into structured text. This section describes how you can insert values computed in a program into a template.
How to do it...
Use the {{.name}}
syntax to substitute a value in a template.
The following code segment executes a template using different inputs:
package main import ( ...