Template packages

Server side languages offers a mechanism for inserting a dynamically generated content into static pages. Go provides a mechanism for substituting the value of variables into text or HTML template.





A Go template is string or file containing text (HTML) and

, that are string expressions enclosed in double braces {{ ... }} that trigger some behaviors. Actions are powerful notations for selecting and printing fields, expressing control flow and calling functions.





Go standard library offers two package for managing templates:

The packages share the same interface so the following examples that treat HTML can also be applied to simple text .

text/template , implements data-driven templates for generating textual output

html/template, is the same as text/template but automatically secures HTML output against certain attacks (script injection) Gin-Gonic Gin, the fast and full-featured web framework for Go that we have already seen in the GIF API post , supports standard template library, so we adopt this framework in our sample application.

