Templating
Although JSON can be the best choice when complex data structures have to be shared across software programs, in general, this is not the case when the HTTP server is supposed to be consumed by humans. In the previous exercises and activities, the chosen way to format a piece of text has been the fmt.Sprintf
function, which is good for formatting texts, but is simply insufficient when more dynamic and complex text is required. As you will have noticed in the previous exercise, the message that was returned in case a name was passed as a parameter to the URL observed a specific pattern, and this is where a new concept comes in – the template. A template is a skeleton from which complex entities can be developed. Essentially, a template is like text with some blanks. A template engine will take some values and fill in the blanks, as shown in the following diagram:
Figure 16.16: Templating example
As you can see, {{name}}
is a placeholder...