{"id":432,"date":"2026-05-25T09:00:00","date_gmt":"2026-05-25T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-html-templates\/"},"modified":"2026-05-25T09:00:00","modified_gmt":"2026-05-25T03:30:00","slug":"go-html-templates","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-html-templates\/","title":{"rendered":"Go HTML Templates: Render Safe Web Pages Easily"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go HTML templates let you build web pages from data, and they escape dangerous characters for you. In this post, you will learn the template syntax and how loops and conditions work. You will also see how to share one layout across pages. Then we will serve a real page with the standard library only.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-structs-beginners-guide\/\">Go structs explained<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-maps-examples\/\">Go maps with examples<\/a>, because templates read fields and keys from them. We also use ideas from <a href=\"https:\/\/smitabharat.com\/blog\/go-http-server-beginners\/\">Go HTTP server basics<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-rate-limiting\/\">Go rate limiting with the token bucket<\/a>. So you may want to skim those first, because the examples build on them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Go HTML Templates?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, a template is a text file with holes in it. The holes are marked with double curly braces, such as <code>{{.Title}}<\/code>. When you run the template with some data, Go fills the holes. Then it writes the finished page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a printed invitation with blank spaces for the guest name and the date. You do not design a new card for each guest. Instead, you print the same card and fill in the blanks. Similarly, Go HTML templates fill the blanks of one page with different data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The package for web pages is <code>html\/template<\/code>. It has the same syntax as <code>text\/template<\/code>, but it adds one big feature. It knows where each value lands in the HTML, so it escapes the value correctly. As a result, it blocks a whole class of attacks. For example, a value inside an attribute is escaped differently from a value inside a script tag.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go HTML Templates Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You could build pages by joining strings, but that quickly gets messy and unsafe. For instance, one forgotten escape can open a security hole. So here is what Go HTML templates give you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Safety by default<\/strong>: values are escaped for HTML, attributes, URLs and JavaScript.<\/li>\n\n\n\n<li><strong>Clean separation<\/strong>: the design lives in a file, and the logic lives in Go.<\/li>\n\n\n\n<li><strong>Reuse<\/strong>: one layout can serve every page on the site.<\/li>\n\n\n\n<li><strong>No extra tools<\/strong>: the package is part of the standard library.<\/li>\n\n\n\n<li><strong>Easy testing<\/strong>: you can render a template into a buffer and check the text.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because of this, Go HTML templates are a solid choice for admin pages, emails and small sites. However, large single-page apps usually use a JavaScript framework instead and serve JSON from Go.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go HTML Templates Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">With Go HTML templates, you first write a template string or file. Next, you parse it with <code>template.New(...).Parse(...)<\/code> or <code>template.ParseFS(...)<\/code>. Then you call <code>Execute<\/code> with a writer and your data. Finally, Go walks through the template, fills each action and writes the HTML.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-1024x640.png\" alt=\"Go HTML Templates diagram showing a template and data going into Execute and safe HTML coming out\" class=\"wp-image-415\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A template with placeholders and a Go data value go into Execute. The output is a finished HTML page with all values escaped.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">However, parsing happens once, while execution happens on every request. So parse your templates at start-up, and then reuse them. As a result, each request only pays for the fill-in step, which is very fast.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Template Actions You Will Use Daily<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Actions are the parts inside <code>{{ }}<\/code>, so the table below lists the most common ones.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Action<\/th><th>What it does<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td><code>{{.Name}}<\/code><\/td><td>Prints a field or map key<\/td><td><code>{{.Title}}<\/code><\/td><\/tr><tr><td><code>{{if .Free}}...{{else}}...{{end}}<\/code><\/td><td>Shows text on a condition<\/td><td>Free or price<\/td><\/tr><tr><td><code>{{range .Items}}...{{end}}<\/code><\/td><td>Loops over a slice or map<\/td><td>A list of courses<\/td><\/tr><tr><td><code>{{range ...}}...{{else}}<\/code><\/td><td>Runs when the list is empty<\/td><td>&#8220;No courses yet&#8221;<\/td><\/tr><tr><td><code>{{upper .Title}}<\/code><\/td><td>Calls a custom function on a value<\/td><td>Upper case title<\/td><\/tr><tr><td><code>{{define \"x\"}}<\/code> and <code>{{block \"x\" .}}<\/code><\/td><td>Builds reusable pieces<\/td><td>Layouts<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Inside <code>range<\/code>, the dot changes to the current item. Therefore, <code>{{.Title}}<\/code> in a loop means the title of the current course. Also, a dash such as <code>{{-<\/code> trims the white space before the action, which keeps the output tidy.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Automatic Escaping<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose a user&#8217;s name contains <code>&lt;script&gt;<\/code>. With string joining, that script would run in other visitors&#8217; browsers. With Go HTML templates, however, the angle brackets turn into harmless text. This protection is called contextual escaping. In fact, it is the main reason to use <code>html\/template<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go HTML Templates vs text\/template<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go has two template packages with almost the same syntax, so the table below shows the difference.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>html\/template<\/th><th>text\/template<\/th><\/tr><\/thead><tbody><tr><td>Escapes output<\/td><td>Yes, by context<\/td><td>No<\/td><\/tr><tr><td>Best for<\/td><td>Web pages and emails<\/td><td>Config files, code generation<\/td><\/tr><tr><td>Safe against script injection<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Syntax<\/td><td>Same<\/td><td>Same<\/td><\/tr><tr><td>Import path<\/td><td><code>html\/template<\/code><\/td><td><code>text\/template<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-1024x640.png\" alt=\"Go HTML Templates diagram comparing html\/template with text\/template\" class=\"wp-image-416\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">The html\/template package turns dangerous characters into safe text, while text\/template prints them exactly as they are.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Use <code>html\/template<\/code> for anything a browser will show, even a small email. On the other hand, use <code>text\/template<\/code> for plain text such as config files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go HTML Templates Hands-On: A Course Page<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build two examples. The first renders a list from a Go slice. The second shares a layout between pages using <code>embed<\/code>. Also, every output below comes from a real run on Go 1.24.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Render a List with Conditions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, here is the data type and the template. The template shows a price, or the word &#8220;Free&#8221;. Also, it uses a custom function to make titles upper case.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Course struct {\n\tTitle string\n\tPrice float64\n\tFree  bool\n}\n\nconst page = `&lt;h1&gt;{{.Heading}}&lt;\/h1&gt;\n&lt;ul&gt;\n{{- range .Courses}}\n  &lt;li&gt;{{.Title | upper}}: {{if .Free}}Free{{else}}${{printf \"%.2f\" .Price}}{{end}}&lt;\/li&gt;\n{{- else}}\n  &lt;li&gt;No courses yet&lt;\/li&gt;\n{{- end}}\n&lt;\/ul&gt;\n&lt;p&gt;{{.Note}}&lt;\/p&gt;\n`<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, the program parses the template with a function map and executes it twice. The second call uses an empty list, so we can see the <code>else<\/code> branch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func main() {\n\tfuncs := template.FuncMap{\"upper\": strings.ToUpper}\n\tt := template.Must(template.New(\"page\").Funcs(funcs).Parse(page))\n\n\tdata := map[string]any{\n\t\t\"Heading\": \"Go Courses\",\n\t\t\"Courses\": []Course{\n\t\t\t{\"Go Basics\", 0, true},\n\t\t\t{\"Go APIs\", 29.5, false},\n\t\t},\n\t\t\"Note\": `&lt;script&gt;alert(\"hacked\")&lt;\/script&gt;`,\n\t}\n\tif err := t.Execute(os.Stdout, data); err != nil {\n\t\tpanic(err)\n\t}\n\n\tempty := map[string]any{\"Heading\": \"Empty\", \"Courses\": []Course{}, \"Note\": \"ok\"}\n\tt.Execute(os.Stdout, empty)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now look at the real output. The note field held a script tag. However, it came out as plain, harmless text.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;h1&gt;Go Courses&lt;\/h1&gt;\n&lt;ul&gt;\n  &lt;li&gt;GO BASICS: Free&lt;\/li&gt;\n  &lt;li&gt;GO APIS: $29.50&lt;\/li&gt;\n&lt;\/ul&gt;\n&lt;p&gt;&amp;lt;script&amp;gt;alert(&amp;#34;hacked&amp;#34;)&amp;lt;\/script&amp;gt;&lt;\/p&gt;\n&lt;h1&gt;Empty&lt;\/h1&gt;\n&lt;ul&gt;\n  &lt;li&gt;No courses yet&lt;\/li&gt;\n&lt;\/ul&gt;\n&lt;p&gt;ok&lt;\/p&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s check the results. First, the free course prints &#8220;Free&#8221;, and the paid course prints <code>$29.50<\/code>. Also, the empty list triggers the <code>else<\/code> branch. Finally, the browser would show the script as text instead of running it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Share a Layout with block and define<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Real sites repeat the same header and footer on every page. So we write a base layout with <code>block<\/code> placeholders, and each page fills them in. Here is <code>templates\/base.html<\/code>, with a simple menu line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{{define \"base\"}}&lt;!doctype html&gt;\n&lt;html&gt;\n&lt;head&gt;&lt;title&gt;{{block \"title\" .}}My Site{{end}}&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n&lt;p&gt;Home | Courses&lt;\/p&gt;\n{{block \"content\" .}}Default content{{end}}\n&lt;\/body&gt;\n&lt;\/html&gt;{{end}}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then <code>templates\/courses.html<\/code> only defines the parts that change.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{{define \"title\"}}Courses{{end}}\n{{define \"content\"}}&lt;h1&gt;{{.Heading}}&lt;\/h1&gt;\n&lt;p&gt;{{len .Items}} courses available.&lt;\/p&gt;{{end}}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Embed the Files and Serve the Page<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, <code>\/\/go:embed<\/code> packs the template files into the binary. As a result, you can deploy a single file with no folders to copy.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/go:embed templates\/*.html\nvar files embed.FS\n\nvar tmpl = template.Must(template.ParseFS(files, \"templates\/base.html\", \"templates\/courses.html\"))\n\nfunc coursesPage(w http.ResponseWriter, r *http.Request) {\n\tdata := map[string]any{\"Heading\": \"Go Courses\", \"Items\": []string{\"Basics\", \"APIs\", \"Testing\"}}\n\tif err := tmpl.ExecuteTemplate(w, \"base\", data); err != nil {\n\t\thttp.Error(w, \"render error\", http.StatusInternalServerError)\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Calling the handler gives this real page.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!doctype html&gt;\n&lt;html&gt;\n&lt;head&gt;&lt;title&gt;Courses&lt;\/title&gt;&lt;\/head&gt;\n&lt;body&gt;\n&lt;p&gt;Home | Courses&lt;\/p&gt;\n&lt;h1&gt;Go Courses&lt;\/h1&gt;\n&lt;p&gt;3 courses available.&lt;\/p&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The title and the content both came from <code>courses.html<\/code>, while the menu line came from the base layout. Because of this, adding a new page means writing only the small parts that differ. For example, an &#8220;About&#8221; page would define its own title and content blocks. It would also reuse the same menu line. Likewise, a change to the layout updates every page at once. In addition, designers can edit the HTML files without touching any Go code at all. This is the biggest time saver in Go HTML templates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go HTML Templates<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>**Using <code>text\/template<\/code> for web pages**: nothing is escaped, so attackers can inject scripts. So always import <code>html\/template<\/code> for HTML.<\/li>\n\n\n\n<li><strong>Parsing on every request<\/strong>: this wastes time. Instead, parse once at start-up.<\/li>\n\n\n\n<li>**Bypassing escaping with <code>template.HTML<\/code>**: this tells Go to trust the string. Therefore, use it only for content you wrote yourself.<\/li>\n\n\n\n<li>**Ignoring the error from <code>Execute<\/code>**: the page may be half written. So check the error and log it.<\/li>\n\n\n\n<li>**Forgetting the dot after <code>block<\/code>**: <code>{{block \"content\" .}}<\/code> passes data down. Without the dot, the child template sees nothing.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go HTML Templates<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Parse templates once<\/strong>, because parsing is the slow part. A package-level <code>template.Must<\/code> call does this at start-up.<\/li>\n\n\n\n<li>**Use <code>embed<\/code>**, since a single binary is easier to deploy.<\/li>\n\n\n\n<li><strong>Keep logic out of templates<\/strong>, so the Go code stays easy to test.<\/li>\n\n\n\n<li><strong>Render into a buffer first<\/strong>, which lets you return a clean 500 if rendering fails.<\/li>\n\n\n\n<li><strong>Build small custom functions<\/strong>, such as date formatting, with <code>FuncMap<\/code>.<\/li>\n\n\n\n<li>**Set the <code>Content-Type<\/code> header**, because browsers need to know the page is HTML.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go HTML templates, build a page that lists blog posts. Each post has a title, an author and a list of tags. Use <code>range<\/code> for the posts and a second <code>range<\/code> for the tags. Then add a function that formats a date.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: pass the data as a slice of structs. After that, add <code>FuncMap{\"date\": func(t time.Time) string {...}}<\/code> and call it with <code>{{.Published | date}}<\/code>. Finally, try a title that contains <code>&lt;b&gt;<\/code> to see the escaping. Then check the page source in your browser to confirm that the tag shows as text.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages explain the template syntax and the embed feature in detail.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pkg.go.dev\/html\/template\">html\/template package documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/text\/template\">text\/template syntax reference<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/embed\">embed package documentation<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go HTML templates fill placeholders in a page with data.<\/li>\n\n\n\n<li>Use <code>html\/template<\/code> for web pages, because it escapes values by context.<\/li>\n\n\n\n<li>Actions such as <code>if<\/code>, <code>range<\/code> and pipelines control what gets printed.<\/li>\n\n\n\n<li><code>define<\/code> and <code>block<\/code> let many pages share one layout.<\/li>\n\n\n\n<li>Parse once at start-up, then call <code>Execute<\/code> on each request.<\/li>\n\n\n\n<li>Use <code>embed<\/code> to ship templates inside the binary.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you can render safe pages. Next, we will look inside the Go runtime and learn how memory and the garbage collector work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up: <a href=\"https:\/\/smitabharat.com\/blog\/go-memory-garbage-collector\/\">Go memory and the garbage collector<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go HTML templates let you build web pages from data, and they escape dangerous characters for you. In this post, you will learn the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":414,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[44,11,81,77],"swapz_series":[],"class_list":["post-432","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-go-http-server","tag-go-lang","tag-go-templates","tag-intermediate"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go HTML Templates: Render Safe Web Pages Easily<\/title>\n<meta name=\"description\" content=\"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-html-templates\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go HTML Templates: Render Safe Web Pages Easily\" \/>\n<meta property=\"og:description\" content=\"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-html-templates\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-25T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go HTML Templates: Render Safe Web Pages Easily\",\"datePublished\":\"2026-05-25T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/\"},\"wordCount\":1458,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-html-templates-featured.png\",\"keywords\":[\"Go HTTP Server\",\"Go Lang\",\"Go Templates\",\"Intermediate\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/\",\"name\":\"Go HTML Templates: Render Safe Web Pages Easily\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-html-templates-featured.png\",\"datePublished\":\"2026-05-25T03:30:00+00:00\",\"description\":\"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-html-templates-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-html-templates-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go HTML Templates featured image: fill the blanks, range and branch, and share a layout\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-html-templates\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go HTML Templates: Render Safe Web Pages Easily\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go HTML Templates: Render Safe Web Pages Easily","description":"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-html-templates\/","og_locale":"en_US","og_type":"article","og_title":"Go HTML Templates: Render Safe Web Pages Easily","og_description":"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.","og_url":"https:\/\/smitabharat.com\/blog\/go-html-templates\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-25T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go HTML Templates: Render Safe Web Pages Easily","datePublished":"2026-05-25T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/"},"wordCount":1458,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png","keywords":["Go HTTP Server","Go Lang","Go Templates","Intermediate"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/","url":"https:\/\/smitabharat.com\/blog\/go-html-templates\/","name":"Go HTML Templates: Render Safe Web Pages Easily","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png","datePublished":"2026-05-25T03:30:00+00:00","description":"Learn Go HTML templates step by step: fill pages with data, loop and branch, escape output safely and share one layout with embed.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-html-templates\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-html-templates-featured.png","width":2400,"height":1260,"caption":"Go HTML Templates featured image: fill the blanks, range and branch, and share a layout"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-html-templates\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go HTML Templates: Render Safe Web Pages Easily"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/414"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=432"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}