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 few chapters from the book.

You will also receive notifications when I release new articles, updates on upcoming courses (including FREE ones), and I'll let you know when my paid courses are on sale.

Avatar of Jon Calhoun
Written by
Jon Calhoun

Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.

Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.

Spread the word

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

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 All Articles Mini-Series Progress Updates Tags About Me Go Courses

©2018 Jonathan Calhoun. All rights reserved.