Skip to main content
The view package provides server-side HTML rendering using Go’s built-in html/template engine. It adds a thin layer on top for layout support, custom functions, and development-friendly features like hot reloading.

What is Server-Side Rendering?

When a user visits your website, the server generates the complete HTML page and sends it to their browser. This is called server-side rendering (SSR).
Benefits of SSR:
  • Fast initial page load (browser displays content immediately)
  • SEO-friendly (search engines can read your content)
  • Works without JavaScript
  • Simple mental model (one language, one place)

What the View Package Provides

How It Works

The view package follows a simple flow:
  1. Configure - Tell the engine where templates are and how to process them
  2. Load - Engine parses and caches your template files
  3. Render - Pass data to a template, get HTML back

Directory Structure

The view package expects templates organized in subdirectories:
  • layouts/ - Templates that wrap around page content (HTML shell, navigation, footer)
  • pages/ - Individual page templates that define content

Quick Example

Here’s a minimal example showing the view package in action: Directory structure:
views/layouts/default.html:
views/pages/home.html:
main.go:
When you visit http://localhost:8080, you’ll see:

Template Data

When you render a template, your data is wrapped in a structure that provides additional context:
In page templates:
In layout templates:

When to Use View

Use the view package when:
  • Building traditional server-rendered websites
  • Creating admin dashboards
  • Building email templates
  • Generating any HTML from Go
Consider alternatives when:
  • Building single-page applications (SPAs) with React/Vue/etc.
  • Building APIs that return JSON
  • Using a separate frontend framework

What’s Next?

  1. Quick Start - Build your first page step by step
  2. Engine - All configuration options explained
  3. Templates - Go template syntax guide
  4. Layouts - Page structure and inheritance
  5. Functions - Built-in and custom helpers
  6. Production - Deployment best practices