Go语言实战案例 — 项目实战篇:简易博客系统(支持评论)

在学习 Go 的过程中,构建一个简易博客系统 是非常典型的练手项目。通过这个项目,我们不仅能掌握 Web 开发的基本流程,还能实现文章发布、浏览、评论互动等核心功能。本文将带你实现一个基于 Go 的简易博客系统,并逐步扩展到支持评论功能。


功能目标

  1. 文章管理

    • 发布文章(标题、内容)
    • 查看文章列表
    • 查看文章详情
  2. 评论功能

    • 在文章下添加评论
    • 展示评论列表
  3. 存储方式

    • 使用内存存储(map/切片)实现简单版本
    • 后续可扩展为文件存储或数据库

技术栈

  • Go 标准库net/httphtml/template
  • 模板渲染:动态生成 HTML 页面
  • 数据存储:使用结构体和切片模拟数据库

项目结构

csharp 复制代码
blog/
├── main.go         # 入口
├── templates/
│   ├── index.html  # 首页文章列表
│   ├── view.html   # 文章详情(含评论)
│   └── new.html    # 新建文章

核心代码(main.go)

go 复制代码
package main

import (
	"html/template"
	"net/http"
	"strconv"
	"sync"
)

// 文章结构体
type Post struct {
	ID      int
	Title   string
	Content string
	Comments []Comment
}

// 评论结构体
type Comment struct {
	Author  string
	Content string
}

// 模拟数据库
var (
	posts = make(map[int]*Post)
	idSeq = 1
	mu    sync.Mutex
)

func main() {
	http.HandleFunc("/", listPosts)
	http.HandleFunc("/post", viewPost)
	http.HandleFunc("/new", newPost)
	http.HandleFunc("/save", savePost)
	http.HandleFunc("/comment", addComment)

	http.ListenAndServe(":8080", nil)
}

// 列出所有文章
func listPosts(w http.ResponseWriter, r *http.Request) {
	tpl := template.Must(template.ParseFiles("templates/index.html"))
	tpl.Execute(w, posts)
}

// 查看文章详情
func viewPost(w http.ResponseWriter, r *http.Request) {
	id, _ := strconv.Atoi(r.URL.Query().Get("id"))
	post, ok := posts[id]
	if !ok {
		http.NotFound(w, r)
		return
	}
	tpl := template.Must(template.ParseFiles("templates/view.html"))
	tpl.Execute(w, post)
}

// 新建文章页面
func newPost(w http.ResponseWriter, r *http.Request) {
	tpl := template.Must(template.ParseFiles("templates/new.html"))
	tpl.Execute(w, nil)
}

// 保存文章
func savePost(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Redirect(w, r, "/", http.StatusSeeOther)
		return
	}
	mu.Lock()
	defer mu.Unlock()
	post := &Post{
		ID:      idSeq,
		Title:   r.FormValue("title"),
		Content: r.FormValue("content"),
	}
	posts[idSeq] = post
	idSeq++
	http.Redirect(w, r, "/", http.StatusSeeOther)
}

// 添加评论
func addComment(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Redirect(w, r, "/", http.StatusSeeOther)
		return
	}
	id, _ := strconv.Atoi(r.FormValue("id"))
	post, ok := posts[id]
	if !ok {
		http.NotFound(w, r)
		return
	}
	comment := Comment{
		Author:  r.FormValue("author"),
		Content: r.FormValue("content"),
	}
	post.Comments = append(post.Comments, comment)
	http.Redirect(w, r, "/post?id="+strconv.Itoa(id), http.StatusSeeOther)
}

模板文件

index.html

html 复制代码
<h1>文章列表</h1>
<a href="/new">写新文章</a>
<ul>
  {{range .}}
  <li><a href="/post?id={{.ID}}">{{.Title}}</a></li>
  {{end}}
</ul>

new.html

html 复制代码
<h1>写新文章</h1>
<form action="/save" method="post">
  标题: <input type="text" name="title"><br>
  内容: <textarea name="content"></textarea><br>
  <button type="submit">发布</button>
</form>

view.html

html 复制代码
<h1>{{.Title}}</h1>
<p>{{.Content}}</p>

<h2>评论</h2>
<ul>
  {{range .Comments}}
  <li><b>{{.Author}}</b>: {{.Content}}</li>
  {{end}}
</ul>

<form action="/comment" method="post">
  <input type="hidden" name="id" value="{{.ID}}">
  昵称: <input type="text" name="author"><br>
  评论: <textarea name="content"></textarea><br>
  <button type="submit">提交评论</button>
</form>

运行效果

  1. 启动服务:
bash 复制代码
go run main.go
  1. 访问 http://localhost:8080
  • 首页显示文章列表
  • 可以发布新文章
  • 点击文章进入详情页,支持评论功能

总结与扩展

我们实现了一个简易博客系统,包括文章的增/查和评论功能,数据存储在内存中。

可扩展的方向:

  • 使用 文件存储SQLite/MySQL 保存数据
  • 添加 用户系统(注册/登录/权限)
  • 评论支持 时间戳、回复、点赞
  • 前端引入 Bootstrap/Vue.js 优化页面

通过这个项目,你不仅能加深对 net/http 和模板渲染的理解,还能体验到 Web 应用开发的完整流程,是 Go 学习过程中非常经典的一个实战案例。

相关推荐
0思必得02 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
东东5162 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino2 小时前
图片、文件的预览
前端·javascript
啊汉3 小时前
古文观芷App搜索方案深度解析:打造极致性能的古文搜索引擎
go·软件随想
layman05284 小时前
webpack5 css-loader:从基础到原理
前端·css·webpack
半桔4 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
AI老李4 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
_OP_CHEN4 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
啊哈一半醒4 小时前
CSS 主流布局
前端·css·css布局·标准流 浮动 定位·flex grid 响应式布局
PHP武器库4 小时前
ULUI:不止于按钮和菜单,一个专注于“业务组件”的纯 CSS 框架
前端·css