Go博客用html/template渲染文章列表需三步:定义导出的Post结构体,创建含{{range .}}遍历逻辑的HTML模板,再在handler中ParseFiles并Execute传入posts切片。

在 Go 语言中开发个人博客系统时,使用 html/template 渲染文章列表是基础且关键的一环。核心在于:组织好数据结构、定义清晰的模板、安全地传递并渲染内容。
准备文章数据结构
先定义一个能表达博客文章的 Go 结构体,确保字段名首字母大写(可导出),以便模板访问:
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Slug string `json:"slug"`
Content string `json:"content"`
CreatedAt time.Time `json:"created_at"`
}
在 HTTP 处理函数中,构造一个文章切片(例如从内存、文件或数据库读取):
posts := []Post{
{ID: 1, Title: "Go 模板入门", Slug: "go-template-basics", CreatedAt: time.Now().Add(-24 * time.Hour)},
{ID: 2, Title: "Markdown 渲染技巧", Slug: "markdown-in-go", CreatedAt: time.Now().Add(-48 * time.Hour)},
}
创建 HTML 模板文件
新建 templates/index.html,使用标准 Go 模板语法遍历文章列表:
立即学习“go语言免费学习笔记(深入)”;
我的博客 文章列表
-
{{range .}}
- {{.Title}} {{.CreatedAt.Format "2006-01-02"}} {{else}}
- 暂无文章 {{end}}
注意:{{range .}} 中的 . 表示传入的整个数据(即 []Post),每轮迭代中 . 自动变为当前 Post 实例;{{else}} 用于空切片兜底。
加载并执行模板
在 handler 中加载模板、解析、然后执行:
func indexHandler(w http.ResponseWriter, r *http.Request) {
// 假设 posts 已准备好
posts := getPosts() // 你的获取逻辑
// 加载模板(支持嵌套、多文件时用 template.ParseGlob)
t, err := template.ParseFiles("templates/index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// 设置响应头(避免浏览器缓存导致模板不更新)
w.Header().Set("Content-Type", "text/html; charset=utf-8")
// 执行模板,传入 posts 切片
if err := t.Execute(w, posts); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}}
如果后续要支持布局复用(如 header/footer),可用 {{define}} 和 {{template}} 拆分模板,并用 template.New("").ParseFiles(...) 统一管理。
安全与实用建议
- 模板中使用
{{.Title | html}}显式转义(虽默认已转义,但明确更稳妥) - 日期格式固定用 Go 的魔术字符串
"2006-01-02",不是 Unix 时间戳或 ISO 格式 - 避免在模板里写复杂逻辑——排序、过滤应在 Go 层完成后再传入
- 开发期可加
log.Println("rendering with", len(posts), "posts")快速验证数据是否到位
不复杂但容易忽略:模板路径错误、结构体字段未导出、时间格式拼错,都会静默失败或渲染空白。










