4.6 Text and HTML Templates
The previous example does only the simplest possible formatting,
for which Printf
is entirely adequate. But sometimes
formatting must be more elaborate, and it’s desirable to separate
the format from the code more completely.
This can be done with the text/template
and
html/template
packages, which provide a mechanism for
substituting the values of variables into a
text or HTML template.
A template is a string or file containing one or more portions enclosed
in double braces, {{...}}
, called actions. Most of the string is printed literally, but the actions trigger other behaviors. Each action contains an expression in the template language, a simple but powerful notation for printing values, selecting struct fields, ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.