前言
写 Go 几乎不需要"装依赖起步"——标准库覆盖了日常大头。这篇按使用频率盘点高频标准库,都是抄了就能用的片段。
一、strings / strconv / unicode
import ("strings"; "strconv"; "unicode")
s := "Hello, Golang 世界"
strings.Contains(s, "Golang") // true
strings.HasPrefix(s, "He") // true
strings.Split("a,b,c", ",") // [a b c]
strings.Join([]string{"a","b"}, "-") // a-b
strings.ToUpper(s)
strings.TrimSpace(" hi \n") // hi
strings.Replace(s, "o", "0", -1) // 全部替换
strings.Fields(" a b c ") // [a b c](按空白切, 超常用)
strings.Repeat("=", 20)
fmt.Sprintf("%s=%d", "k", 1)
// Builder: 循环拼字符串用它(比 += 快得多)
var b strings.Builder
for i := 0; i < 1000; i++ {
b.WriteString("x")
}
b.String()
// strconv: 字符串与数字互转
strconv.Itoa(42) // "42"
n, err := strconv.Atoi("42")
f, err := strconv.ParseFloat("3.14", 64)
二、slices / maps / sort(go1.21+ 泛型库)
import ("slices"; "maps"; "sort")
nums := []int{3, 1, 4, 1, 5}
slices.Contains(nums, 4) // true
slices.Index(nums, 4) // 2
slices.Sort(nums)
slices.Reverse(nums)
slices.Equal([]int{1,2}, []int{1,2}) // true
slices.Max(nums), slices.Min(nums) // 5, 1
// 结构体排序
type User struct{ Name string; Age int }
users := []User{{"b", 30}, {"a", 25}}
slices.SortFunc(users, func(x, y User) int { return x.Age - y.Age })
// maps
m := map[string]int{"a": 1, "b": 2}
keys := slices.Collect(maps.Keys(m)) // 迭代器收集
maps.Clone(m) // 浅拷贝
三、time:时间处理
now := time.Now()
now.Format("2006-01-02 15:04:05") // 记住: Go 的格式化模板就是这个参考时间!
t, err := time.Parse("2006-01-02", "2024-11-16")
now.Add(24 * time.Hour) // 明天
now.Sub(startTime) // time.Duration
d := 2*time.Hour + 30*time.Minute
d.Seconds() // 9000
// 休眠与超时
time.Sleep(100 * time.Millisecond)
// 定时器
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for range ticker.C {
fmt.Println("每5秒执行", time.Now())
}
// 耗时测量
start := time.Now()
heavyWork()
log.Printf("cost %v", time.Since(start)) // 1.234ms
四、encoding/json(序列化基石)
type Server struct {
Host string `json:"host"`
Port int `json:"port,omitempty"` // 零值时省略
Tags []string `json:"tags,omitempty"`
}
// 结构体 -> JSON
s := Server{Host: "10.0.0.1", Port: 8080, Tags: []string{"prod"}}
data, _ := json.Marshal(s)
data2, _ := json.MarshalIndent(s, "", " ") // 带缩进(好读)
// JSON -> 结构体
var s2 Server
json.Unmarshal([]byte(`{"host":"h1","port":80}`), &s2)
// JSON -> map(结构未知时)
var m map[string]any
json.Unmarshal([]byte(`{"a":1}`), &m)
五、net/http:自带 Web 能力
// 服务端: 十行起一个生产可用的 API
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "ok")
})
http.HandleFunc("/api/hello", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name") // ?name=zy
if name == "" {
http.Error(w, "missing name", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"msg": "hello " + name})
})
log.Println("listen :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
// 客户端
resp, err := http.Get("https://httpbin.org/get")
if err != nil { log.Fatal(err) }
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println(resp.StatusCode, len(body))
// 带超时的 client(生产必配! 默认没有超时)
client := &http.Client{Timeout: 5 * time.Second}
六、os / os/exec / filepath
import ("os"; "os/exec"; "path/filepath")
// 文件
data, err := os.ReadFile("app.conf")
os.WriteFile("out.txt", []byte("hi"), 0644)
f, _ := os.Open("big.log")
defer f.Close()
scanner := bufio.NewScanner(f) // 逐行读(大文件)
for scanner.Scan() { fmt.Println(scanner.Text()) }
// 目录
entries, _ := os.ReadDir("/var/log")
for _, e := range entries { fmt.Println(e.Name(), e.IsDir()) }
// 路径( filepath 跨平台)
filepath.Join("/etc", "app", "a.conf") // /etc/app/a.conf
filepath.Ext("a.tar.gz") // .gz
filepath.Base("/x/y/z.txt") // z.txt
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && filepath.Ext(path) == ".log" { fmt.Println(path) }
return nil
})
// 执行命令
out, err := exec.Command("df", "-h").Output()
if ee, ok := err.(*exec.ExitError); ok {
log.Println("exit code:", ee.ExitCode())
}
七、flag / env:程序配置
import "flag"
port := flag.Int("port", 8080, "监听端口")
env := flag.String("env", "dev", "运行环境")
flag.Parse()
fmt.Println(*port, *env)
// ./app -port 9090 -env prod
// 环境变量
if v := os.Getenv("APP_ENV"); v == "" {
os.Setenv("APP_ENV", "dev") // 或 log.Fatal("必须设置 APP_ENV")
}
八、context(超时与取消,下篇细讲,先混个脸熟)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil) // 请求级超时
resp, err := client.Do(req)
九、综合:一个小工具
// urlcheck.go —— 批量探测 URL 可用性
package main
import (
"context"; "fmt"; "net/http"; "os"; "sync"; "time"
)
func main() {
client := &http.Client{Timeout: 3 * time.Second}
urls := os.Args[1:]
var wg sync.WaitGroup
for _, u := range urls {
wg.Add(1)
go func(u string) {
defer wg.Done()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", u, nil)
_, err := client.Do(req)
status := "OK"
if err != nil { status = "FAIL: " + err.Error() }
fmt.Printf("%-40s %s\n", u, status)
}(u)
}
wg.Wait()
}
// go run urlcheck.go https://a.com https://b.com
十几行标准库 = 并发 + 超时 + CLI 工具,Go 的"自带电池"含金量。
小结
| 库 | 高频点 |
|---|---|
| strings/strconv | Fields/Split/Builder/Atoi |
| slices/maps | 泛型工具,新代码优先 |
| time | Format 参考时间模板、Since 计时 |
| encoding/json | Marshal/Unmarshal + tag |
| net/http | 服务端 HandleFunc、客户端带 Timeout |
| os/exec/filepath | 文件/命令/路径 |
| flag | 命令行参数 |
本文是「Golang」系列第 7 篇。