1214 字
6 分钟
08 · 标准库实战:net/http、encoding/json、log/slog、flag
2026-10-24
NOTE

很多人上手 Go 就先装 Gin。其实标准库能覆盖 80% 的服务需求,而且升级不用等框架适配。 这篇把四个最常用的包讲透:net/http、encoding/json、log/slog、flag。


1. net/http:一个接口撑起全部#

type Handler interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}

任何实现了这一个方法的类型,都能当 handler。http.HandlerFunc 是它的函数适配器:

func hello(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, "hello")
}
http.HandleFunc("/hello", hello) // 注册到 DefaultServeMux
http.ListenAndServe(":8080", nil)

中间件的本质就是”包一层”#

type Middleware func(http.Handler) http.Handler
func withLogging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
next.ServeHTTP(w, r)
slog.Info("request", "method", r.Method, "path", r.URL.Path, "cost", time.Since(start))
})
}

链式串联(从外到内):

var h http.Handler = http.HandlerFunc(hello)
h = withLogging(h)
h = withRecover(h)

路由:Go 1.22 起 ServeMux 支持方法与路径参数#

mux := http.NewServeMux()
mux.HandleFunc("GET /tasks/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id") // 直接取路径参数,不用第三方路由
// ...
})
mux.HandleFunc("POST /tasks", createTask)

不需要 gin 也能有 GET /tasks/{id} 这种语义。复杂匹配(正则、通配)才考虑 chi。

写响应:三种常见形态#

// JSON
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(task)
// 纯文本 + 状态码
http.Error(w, "not found", http.StatusNotFound)
// 空响应
w.WriteHeader(http.StatusNoContent)

⚠️ WriteHeader 只能调一次,之后再写 header 无效;json.NewEncoder(w) 会隐式写 200,所以要先 WriteHeader 再 Encode。


2. encoding/json:协议就是 tag#

type Task struct {
ID string `json:"id"`
Title string `json:"title"`
Status string `json:"status,omitempty"` // 零值时不输出
CreatedAt time.Time `json:"createdAt"`
Internal string `json:"-"` // 永不输出
}

几个关键点:

需求写法
改字段名json:"camelCase"
零值省略json:"x,omitempty"
不输出json:"-"
字符串数字json:"n,string"
时间格式默认 RFC3339;要别的格式得自定义类型

自定义时间格式(前端要 yyyy-MM-dd HH:mm:ss 时很常见):

type LocalTime time.Time
func (t LocalTime) MarshalJSON() ([]byte, error) {
return []byte(`"` + time.Time(t).Format("2006-01-02 15:04:05") + `"`), nil
}
func (t *LocalTime) UnmarshalJSON(data []byte) error {
parsed, err := time.Parse(`"2006-01-02 15:04:05"`, string(data))
if err != nil {
return err
}
*t = LocalTime(parsed)
return nil
}

解码要注意的三件事:

  1. json.Unmarshal 不认识多余字段(默认忽略);要严格就 dec.DisallowUnknownFields()。
  2. 传指针:json.Unmarshal(data, &v),传值会改副本。
  3. 大响应别用 Unmarshal,用 json.Decoder 流式解码省内存。
dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields() // 前端多传字段直接报错(配合白名单防越权字段)
if err := dec.Decode(&input); err != nil {
// 返回 400
}

3. log/slog:结构化日志(Go 1.21+)#

slog.Info("user login",
"userID", u.ID,
"ip", r.RemoteAddr,
"cost", time.Since(start),
)

输出(默认 TextHandler):

time=2026-10-24T21:00:00.000+08:00 level=INFO msg="user login" userID=u-1 ip=1.2.3.4 cost=12ms

切 JSON(生产推荐,便于采集):

handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})
log := slog.New(handler)
slog.SetDefault(log)

几个实用能力:

// 带固定字段的子 logger(一次设置,处处携带)
reqLog := slog.With("requestID", rid, "path", r.URL.Path)
reqLog.Info("start")
reqLog.Warn("slow query", "cost", cost)
// 分组
slog.Info("db", "query", slog.Group("sql", "table", "tasks", "rows", 12))
// 错误
slog.Error("create task failed", "err", err)

脱敏:slog 没有内置 redact,得自己控制——不要在日志里打 Authorization、Cookie、密码、完整手机号。


4. flag + 环境变量:配置的两层来源#

var (
addr = flag.String("addr", ":8080", "监听地址")
dsn = flag.String("dsn", os.Getenv("DATABASE_URL"), "数据库连接串(也可用环境变量)")
)
func main() {
flag.Parse()
if *dsn == "" {
slog.Error("缺少 DATABASE_URL")
os.Exit(1)
}
// ...
}

约定:

  • 环境相关(端口、DSN、密钥)走环境变量,方便容器部署;
  • 临时开关(调试、迁移)走 flag,命令行试;
  • 默认值要能让本地”零配置跑起来”(比如 :8080 + 本地 sqlite)。

5. 其它常被忽略但很有用的包#

包用途一句话示例
io通用读写接口io.Copy(dst, src)
bufio带缓冲读写w := bufio.NewWriter(f),最后 w.Flush()
context取消与超时见第 06 篇
errors错误包装与判等见第 04 篇
path/filepath跨平台路径filepath.Join("a", "b")
time时间与定时time.Now().UTC()
sort / slices排序slices.Sort(nums)(1.21+)
mapsmap 工具maps.Keys(m)、maps.Clone(m)

常见坑速查#

坑症状做法
WriteHeader 调两次日志警告 superfluous WriteHeader只写一次,注意 Encoder 隐式 200
忘了 Content-Type浏览器/前端按文本解析显式设置
json.Unmarshal 传值解码无效传 &v
omitempty 对数字 0 生效想输出 0 却丢了用指针字段或用自定义 Marshal
日志里打了 token安全事故白名单字段,敏感字段脱敏
flag 在库代码里用与使用方的 flag 冲突只在 main 里解析
bufio.Writer 忘 Flush输出丢一半defer Flush

小结#

  • net/http 的核心就一个 Handler 接口,中间件就是”包一层”。
  • Go 1.22 的 ServeMux 已支持 GET /tasks/{id},多数项目不需要路由库。
  • encoding/json 用 tag 控协议;解码传指针;大响应用 Decoder。
  • log/slog 是结构化日志的标准答案,生产切 JSON handler。
  • 配置:环境变量为主,flag 为临时开关。

练习#

  1. 用标准库起一个服务,提供 GET /hello(返回 JSON)与 GET /users/{id}(用 PathValue 取参),用 curl 验证。
  2. 写一个日志中间件:记录方法、路径、状态码、耗时;状态码要靠自己包一层 ResponseWriter 才能拿到(提示:实现 WriteHeader 并记录)。
  3. 给 Task 写自定义时间格式(2006-01-02 15:04:05),验证序列化与反序列化都对。
  4. 用 DisallowUnknownFields 让”多传一个 role 字段”的请求返回 400。
  5. 把日志从 TextHandler 换成 JSONHandler,用 jq 过滤出 level=ERROR 的行。
08 · 标准库实战:net/http、encoding/json、log/slog、flag
https://haolo.cloud/blog/posts/golang-roadmap-08-stdlib-http-json-log/
作者
Ethan
发布于
2026-10-24
许可协议
CC BY-NC-SA 4.0
本系列目录
Go学习路线 · 共 14 篇 · 当前第 9 篇
  1. 0 总览:Go 这条路怎么走(写给有编程基础的人)
  2. 1 环境与工具链:从 go mod 开始就走在队形里
  3. 2 类型系统与值语义:Go 真正让人栽跟头的地方
  4. 3 接口与组合:没有 implements,面向对象换了个样子
  5. 4 错误处理:error 是值,不是异常
  6. 5 并发(上):goroutine 与 channel,先学会不泄漏
  7. 6 并发(下):sync 与 context,把"能跑"变成"靠谱"
  8. 7 内存与性能:先有数字,再谈优化
  9. 8 标准库实战:net/http、encoding/json、log/slog、flag 当前
  10. 9 写一个 Web 服务:路由、中间件、统一错误、优雅关闭
  11. 10 数据层:database/sql 的正确姿势(连接池、事务、迁移)
  12. 11 测试与工程质量:table-driven、httptest、-race 与 CI
  13. 12 毕业项目:用标准库写一个能上线的任务 API(与前端同一份契约)
  14. 13 泛型与约束:Go 1.18+ 的类型参数
查看路线总览