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).- 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
| Feature | Description |
|---|---|
| Template Engine | Wraps Go’s html/template with convenience methods |
| Layouts | Wrap page content in a consistent structure |
| Custom Functions | Add your own template helpers |
| Hot Reload | Templates reload automatically during development |
| Embedded Files | Bundle templates in your binary for production |
How It Works
The view package follows a simple flow:- Configure - Tell the engine where templates are and how to process them
- Load - Engine parses and caches your template files
- 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:http://localhost:8080, you’ll see:
Template Data
When you render a template, your data is wrapped in a structure that provides additional context: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
- Building single-page applications (SPAs) with React/Vue/etc.
- Building APIs that return JSON
- Using a separate frontend framework
What’s Next?
- Quick Start - Build your first page step by step
- Engine - All configuration options explained
- Templates - Go template syntax guide
- Layouts - Page structure and inheritance
- Functions - Built-in and custom helpers
- Production - Deployment best practices