An Introduction to Templates in Go

This series introduces the html/template package included in Go’s standard library, and then continues to talk about various aspects of it including contextual encoding, how to use actions like if/else and other, how to use functions and define new ones to be made available in your templates, and finally how to put all together to create a reusable view layer.

1.Contextual Encoding

In the first article of this series we focus on creating a relatively simple, logicless template and explore how using variable data in different contexts can result in different outputs. This is called contextual encoding, and is a really neat feature provided by the Go templating packages (most notably the html/template package).

2.Template Actions (if/else, range, etc)

After creating some basic templates we are ready to start adding logic to them. This article explains how to use some of the more common actions like if/else blocks and ranges to iterate over slices and maps. Finally we look at how to embed templates, since this is incredibly helpful for avoiding constant repition in templates.

3.Using Functions Inside Templates

Actions allow us to perform some basic operations inside of our templates, but at some point they likely won’t be enough. In this article we discuss how to use functions inside of our templates, as well as how to provide custom functions that enable us to write much simpler template code while not giving up any flexibility in how we render dynamic content.

4.Creating the V in MVC

In this article we learn how to create a reusable view layer with things we have learned in previous articles plus a few new techniques. This includes creating a shared layout, defining default templates that can be overridden, and including the same templates across various pages without putting all of the code into a single file.

Even if you don’t use MVC in your applications (I don’t), this is still an incredibly useful article in learning how to build reusable views to simplify HTML output.

Want to see how templates work in the bigger picture?

In my course - Web Development with Go - we use the html/template package to build out an entire view layer for a realistic application. If you have ever wondered how all of these pieces fit together in the scope of a complete web application, I suggest you check out the course.

If you sign up for my mailing list (down there ↓) I'll send you a FREE sample so you can see if it is for you. The sample includes over 2.5 hours of screencasts and the first three chapters from the book.

You will also receive notifications when I release new articles, along with other freebies that I only share with my mailing list.

Avatar of Jon Calhoun
Written by
Jon Calhoun

Jon Calhoun is a full stack web developer who also teaches about Go, web development, algorithms, and anything programming related. He also consults for other companies who have development needs. (If you need some development work done, get in touch!)

Jon is a co-founder of EasyPost, a shipping API that many fortune 500 companies use to power their shipping infrastructure, and prior to founding EasyPost he worked at google as a software engineer.

Spread the word

Did you find this page helpful? Let others know about it!

Vote on Hacker News

Sharing helps me continue to create both free and premium Go resources.

Want to discuss the article?

See something that is wrong, think this article could be improved, or just want to say thanks? I'd love to hear what you have to say!

You can reach me via email or via twitter.

Recent Articles Mini-Series Tags About Me Go Courses

©2018 Jonathan Calhoun. All rights reserved.