3 Simple Techniques using Go Templates

Using Go Templates can be quite confusing at the start but once you get used to what all it can do, you will simply love it. Right from printing simple text or parsing the text into an HTML to display it on a web page or to send it via email is what Go Templates can do.

I am going to show you 6 simple tricks in with how does each functionality work and a sample working code of each of those simple tricks in Go with Github Gist Page and Go Playground link.

Let us get started-

1. Using functions in Go templates

You can use functions to do any kind of formatting you need in the template.

Here are some examples where you can use functions in Go Templating:

  • String Manipulation

In Go Templates, you can use any string functions you need to be used in your text/HTML templates. All you need is to use the FuncMap map[string]func which maps your template function to the actual strings library function. For example, please look at line no. 18–20 in the below example.

Just like below, "ToUpper" links to "toUpper”of the strings library function. Now how do you use this "ToUpper" function in your template? You can simply pipe the function to any string you want to manipulate. For example, please look at line no. 23–26 for the pipes in the below example.

Using functions in Go Templates for String Manipulation

Here is the output for the above piece of code,

This Is A Title
THIS IS AN UPPER CASE SENTENCE
this is a lower case sentence

Go Playground link: https://play.golang.org/p/nPrTY0iTZwa

  • Time Formatting

You can also time format your time in any format you need. You just need to follow the conventions go has set for a date, month, year, hours, minutes, seconds, time zone, etc. To know more about the conventions, please click here.

In the below example, we are formatting the current time to 02 Jan 2006 15:04:05 MST .

Here is the output for the above piece of code,

% go run timeformatting.go 
29 Mar 2021 09:16:32 PDT

Go Playground Link: https://play.golang.org/p/Gn3EEzf3lt8

  • Struct-specific function calling

You can also call the functions of a particular type. For example, below is the code where the code prints out Manager: Yes if the employee is a manager or Manager: No if the employee is not a manager.

Here is the output for the above:

Name: John Doe
Email Address: john.doe@mailer.com
Manager: No

Name: Chloe Dowd
Email Address: chloe.dowd@mailer.com
Manager: Yes

Go playground: https://play.golang.org/p/EJP8OhgkUQx

2. Use “-” to trim white space

Using “-” can be super useful when you have a lot of embedded data in your HTML code and you do not want to deal with extra white spacing before or after your embedded statements. For example, in the below code line 19, 20 and 21 specify Name: , Email Address: and Phone Number: . If you keenly observe, all the above text has a “space” after the “colon”. That specify gets trimmed if you type - before the attribute specification. Same goes if you need trim space after the attribute specification. All you need to do is type - after the attribute specification and before }} .

Here is the output for the above code:

% go run usetrim.goName:John Doe
Email Address:john.doe@hi.com
Phone Number:+19876543210
Name:Fredrika Gaila
Email Address:fredrika.gaila@hi.com
Phone Number:+11234567890

3. Using Nested Templates

Nested templates are something where you need to reuse a set of templates within different templates. Say, you have a header and a footer that get used all the time in different HTML pages on your website and that website uses go templates. Will you have your header and footer be declared on each and every HTML page on your website? The answer is a simple NO! That would be cumbersome if there needs to be a change to the header or footer. Using the above approach will make us update all the HTML files which is not the right way to do it. Instead, we may have a header and footer in separate files and import them in requesting HTML pages.

I have customized a simple example for you all in the following Github gist:

I have printed out the output in the console for now, but here is how the output looks:

krishbhanushali@Krish templating % go run main.goYo! I am the header.This is the content in main.htmlYo! I am the footer.

I hope this article helps. Thank you so much for reading. :) Cheers!

Analytic problem solver by birth and programmer by choice. Motivator and spiritual. Portfolio: www.krishbhanushali.me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store