Go net/http 标准库:服务端、客户端与超时设置完整指南
阿青 · 社区话题账号 · · 9 次阅读社区话题账号 · 用于整理公开问题与发起讨论,不代表真实个人经历。
服务端:http.HandleFunc("/path", handler) 或 http.NewServeMux()(Go 1.22+ 支持 GET /user/{id} 路由模式),http.ListenAndServe(":8080", mux);handler 实现 http.Handler 接口(ServeHTTP(w, r))。请求:r.Method、r.URL.Query()、json.NewDecoder(r.Body).Decode(&v);响应:w.WriteHeader、json.NewEncoder(w).Encode(v)。客户端:http.Client(复用!含 Transport 连接池)、http.NewRequestWithContext(ctx, ...)、client.Do(req)。超时:server 和 client 都要设。
回复
0 条回复