标准库实战
Go 标准库功能丰富,日常开发大部分需求无需第三方库。本章介绍最常用的几个包。
net/http — HTTP 服务与客户端
简单 HTTP 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package main
import ( "fmt" "net/http" )
func helloHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) }
func main() { http.HandleFunc("/hello/", helloHandler) http.ListenAndServe(":8080", nil) }
|
JSON API
1 2 3 4 5 6 7 8 9
| type Response struct { Code int `json:"code"` Message string `json:"message"` }
func userHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(Response{Code: 0, Message: "ok"}) }
|
HTTP 客户端
1 2 3 4 5 6 7
| resp, err := http.Get("https://api.example.com/users") if err != nil { return err } defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
|
生产环境建议使用带 Context 和超时的 http.Client:
1 2 3
| client := &http.Client{Timeout: 10 * time.Second} req, _ := http.NewRequestWithContext(ctx, "GET", url, nil) resp, err := client.Do(req)
|
encoding/json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| type User struct { ID int64 `json:"id"` Name string `json:"name"` }
data, err := json.Marshal(user)
var u User err = json.Unmarshal(data, &u)
decoder := json.NewDecoder(r.Body) encoder := json.NewEncoder(w)
|
database/sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import ( "database/sql" _ "github.com/go-sql-driver/mysql" )
db, err := sql.Open("mysql", "user:pass@tcp(localhost:3306)/dbname") if err != nil { log.Fatal(err) } defer db.Close()
db.SetMaxOpenConns(25) db.SetMaxIdleConns(5)
row := db.QueryRowContext(ctx, "SELECT name FROM users WHERE id = ?", id) err = row.Scan(&name)
|
io 与 os
1 2 3 4 5 6 7 8 9 10 11
| data, err := os.ReadFile("config.yaml")
err = os.WriteFile("output.txt", data, 0644)
io.Copy(dst, src)
limited := io.LimitReader(r, 1024)
|
time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| now := time.Now() t := time.Date(2026, 6, 29, 12, 0, 0, 0, time.Local)
layout := "2006-01-02 15:04:05" s := now.Format(layout) t, err := time.Parse(layout, "2026-06-29 12:00:00")
timer := time.NewTimer(5 * time.Second) <-timer.C
ticker := time.NewTicker(time.Second) defer ticker.Stop() for range ticker.C { }
|
strings 与 strconv
1 2 3 4 5 6 7 8
| strings.Contains(s, "sub") strings.Split(s, ",") strings.Join([]string{"a", "b"}, ",") strings.Builder
i, err := strconv.Atoi("42") s := strconv.Itoa(42) f, err := strconv.ParseFloat("3.14", 64)
|
小结
net/http 足以构建 REST API 和微服务
encoding/json 处理 JSON 序列化
database/sql + 驱动连接数据库,注意连接池配置
- 优先使用标准库,第三方库选型需评估维护活跃度