Go

From znlgis/opengis-skills · View interactive version

Go is an AI agent skill from the GitHub repository znlgis/opengis-skills. Use when writing or debugging Go (go1.27.4) — goroutines/channels and context cancellation, net/http and ServeMux patterns, database/sql with MySQL/Postgres drivers, generics, go mod tidy / go work, table-driven tests and -race. Also for \"all goroutines are asleep - deadlock!\", data races on map w

How to use this skill

Skill file

官网: <https://go.dev/>

项目地址: <https://github.com/golang/go>

官方文档: <https://go.dev/doc/>

Go by Example: <https://gobyexample.com/>

Effective Go: <https://go.dev/doc/effective_go>

许可证: BSD-3-Clause

包管理器: 官方二进制 tar.gz / brew install go / winget install Golang.go(最新 go1.27.4,截至 2026-11)

版本: go1.27.4(2026-11-02 发布,截至 2026-11,以 <https://go.dev/dl/?mode=json> 返回为准;<https://go.dev/doc/devel/release> 记录 1.27.0 起各补丁版)

概述

Go 是由 Robert Griesemer、Rob Pike 和 Ken Thompson 于 2007 年在 Google 发起的编程语言,2009 年首次公开发布,2012 年发布 Go 1.0。核心设计目标:简洁、高效、安全、并发优先、工程化。

| 特性 | 说明 |

|------|------|

| 静态类型 + 编译型 | 无运行时解释开销,编译为单文件原生机器码 |

| 垃圾回收 | 并发三色标记清除 GC,亚毫秒级 STW |

| 内置并发 | goroutine(轻量线程,2KB 初始栈)+ channel(CSP 模型) |

| 快速编译 | 增量编译 + 包缓存,大型项目秒级构建 |

| 丰富标准库 | net/http、database/sql、encoding/json、context 等开箱即用 |

| 统一代码风格 | gofmt 内置格式化,无风格争议 |

| 模块系统 | Go Modules(go.mod),语义版本控制 |

| 泛型 | Go 1.18+ 支持类型参数 |

| 工具链 | testing、benchmark、pprof、race detector 内置 |

版本里程碑

| 版本 | 年份 | 关键特性 |

|------|------|---------|

| 1.0 | 2012 | 首个稳定版 |

| 1.7 | 2016 | context 正式入标准库 |

| 1.11 | 2018 | Go Modules 引入 |

| 1.13 | 2019 | error wrapping(%w) |

| 1.18 | 2022 | 泛型、模糊测试、workspace(go.work) |

| 1.21 | 2023 | slices/maps 标准库、log/slog、atomic 类型、cmp.Ordered |

| 1.22 | 2024 | 循环变量每次迭代新作用域、增强 ServeMux 路由 |

| 1.23 | 2024 | range over func(迭代器函数)、unique 包 |

| 1.24 | 2025 | 泛型类型别名、Swiss Table map 优化、weak 指针 |

| 1.25 | 2025 | 容器感知 GOMAXPROCS、testing/synctest、trace flight recorder、实验性新 GC 与实验性 encoding/json/v2 |

| 1.26 | 2026 | 新垃圾回收器、cgo 调用加速、堆基址随机化、实验性 goroutine 泄漏剖析、crypto/hpke、实验性 simd/archsimd 与 runtime/secret |

| 1.27 | 2026 | 内存分配提速、encoding/json/v2 与 encoding/json/jsontext 正式加入标准库、crypto/mldsa(后量子)、uuid 包、实验性 simd |


环境准备

安装

# macOS
brew install go

# Linux(官方二进制)
wget https://go.dev/dl/go1.27.3.linux-amd64.tar.gz
sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.27.3.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

# Windows
winget install Golang.go

环境变量

go env GOROOT          # Go 安装目录
go env GOPATH          # Go 工作区(默认 ~/go)
go env GOPROXY         # 模块代理(国内推荐 https://goproxy.cn)
go env GOSUMDB         # 校验数据库(默认 sum.golang.org)
go env GO111MODULE     # 模块模式(on,Go 1.16 起默认开启)

# 国内加速
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.google.cn

IDE 推荐


基础语法、方法与接口、并发、错误处理与泛型的完整讲解见 reference/language-basics.md

包管理与工程化、标准库速查、Web 开发、数据库与测试的完整内容见 reference/stdlib-web-db.md

核心 API / 标准库速查

| 主题 | 入口 | 说明 |

|------|------|------|

| 并发 | go f() / chan T / select | goroutine + channel(CSP);sync.WaitGroup、sync.Mutex、sync.Once、sync.Map |

| 取消与超时 | context.Context | context.WithCancel/WithTimeout/WithValue,I/O 函数第一个参数传 ctx |

| HTTP 服务 | net/http | http.HandleFunc / http.ServeMux(1.22+ 支持方法与路径模式)/ http.Client |

| JSON | encoding/json | json.Marshal/Unmarshal、struct tag;encoding/json/v2 与 encoding/json/jsontext 自 1.27 进入标准库(1.25 需 GOEXPERIMENT=jsonv2) |

| 数据库 | database/sql | sql.Open + 驱动(如 go-sql-driver/mysql)、QueryContext/ExecContext |

| 错误处理 | errors + fmt | fmt.Errorf("...: %w", err) 包装、errors.Is/As/Join |

| 泛型 | 类型参数(方括号) | func Map[T, U any] 这样声明,形参照常用圆括号;调用时 Map[int, string] 可省略部分推断 |

| 集合工具 | slices / maps / cmp | slices.Sort、maps.Clone、cmp.Compare(1.21+) |

| 日志 | log/slog | 结构化日志(1.21+),slog.Info("msg", "k", v) |

| 测试 | testing | table-driven 测试、t.Run 子测试、go test -race/-bench;testing/synctest(1.25 新增,测并发时序) |

| 性能剖析 | runtime/pprof / net/http/pprof | CPU/内存/阻塞剖析 |

| 模块 | go mod | go mod init/tidy、go get、go work(1.18+ workspace) |


常用生态库

| 库 | 用途 | 仓库 |

|----|------|------|

| gin | 高性能 Web 框架,路由与中间件 | <https://github.com/gin-gonic/gin> |

| testify | 测试断言与 mock(assert/require/mock) | <https://github.com/stretchr/testify> |

| lib/pq | PostgreSQL 驱动(database/sql) | <https://github.com/lib/pq> |

| go-sql-driver/mysql | MySQL 驱动(database/sql) | <https://github.com/go-sql-driver/mysql> |

| go-redis | Redis 客户端 | <https://github.com/redis/go-redis> |

| gohook | 键鼠事件钩子(桌面自动化) | <https://github.com/robotn/gohook> |


典型工作流

构建一个包含数据库和中间件的 Go REST API 服务

# 1. 初始化模块
go mod init example.com/myapi

# 2. 添加依赖
go get github.com/go-chi/chi/v5
go get github.com/go-sql-driver/mysql
go get github.com/redis/go-redis/v9
// 3. cmd/api/main.go
package main

import (
    "log"
    "net/http"
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

func main() {
    r := chi.NewRouter()

    // 中间件
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Use(middleware.Timeout(30 * time.Second))

    // 路由
    r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(`{"status":"ok"}`))
    })

    r.Route("/api/users", func(r chi.Router) {
        r.Get("/", listUsers)
        r.Post("/", createUser)
        r.Get("/{id}", getUser)
    })

    log.Println("Server on :8080")
    log.Fatal(http.ListenAndServe(":8080", r))
}
# 4. 运行
go run ./cmd/api

常见问题

| 陷阱 | 说明 | 解决方案 |

|------|------|---------|

| 切片共享底层数组 | s2 := s1[2:5] 后修改 s2 影响 s1 | 用 copy() 创建独立副本 |

| 循环变量捕获(Go <1.22) | 闭包捕获的循环变量引用同一地址 | Go 1.22+ 已修复;旧版传参 func(i int) |

| nil 接口不等于 nil | var p *T; var i error = p; i != nil | 检查 if p == nil 而非接口 |

| nil map 写入 panic | var m map[string]int; m["a"]=1 panic | 先 m = make(map[string]int) |

| defer 在循环中 | 大量 defer 堆积不执行 | 提取为函数或立即调用 |

| map 并发写 panic | 多 goroutine 同时写同一 map | 用 sync.Map 或加锁 |

| 浮点数比较 | 0.1 + 0.2 != 0.3 | 使用 tolerance 比较 |

| fatal error: all goroutines are asleep - deadlock! | 所有 goroutine 都在等 channel/锁,无人推进 | 检查 unbuffered channel 的收发是否成对、WaitGroup 是否漏 Done();用 go test -timeout 10s 快速暴露 |

| fatal error: concurrent map writes | 多个 goroutine 同时写同一 map,运行时检测到即崩溃 | 用 sync.Mutex/sync.RWMutex 保护,或改用 sync.Map;go test -race / go run -race 能提前定位 |

| missing go.sum entry for module ...; to add it: go mod download | 新增依赖后没同步校验和,或 go.sum 被清理 | 执行 go mod tidy(会补齐 go.sum);CI 里加 go mod tidy -diff 防止漏提交 |

| undefined: errors.Join / undefined: slices.Sort 等标准库符号 | 工具链版本低于该 API 引入的版本,或 go 指令行写了旧版本 | go version 对齐 go1.27.3;把 go.mod 的 go 1.27 指令改成实际工具链支持的最低版本 |

| build constraints exclude all Go files | 目标文件全被 //go:build 标签排除(平台/架构不符) | 用 GOOS/GOARCH 匹配标签,或检查文件名后缀(如 _windows.go);go list -f '{{.GoFiles}}' 看实际纳入的文件 |


AI 使用建议

  1. 优先用标准库:Go 标准库覆盖面广(net/http、database/sql、encoding/json)。评估第三方依赖前先检查标准库。
  2. 错误必须处理:Go 没有 try/catch。每个 error 返回值都必须检查,避免 _ 忽略。
  3. 用 context 传播取消:所有 I/O 操作应接受 context.Context 参数,支持超时和取消。
  4. goroutine 防泄漏:确保每个 goroutine 都有退出路径(channel 关闭 / context 取消 / done 信号)。
  5. 先写测试:table-driven 测试是 Go 社区标准实践。
  6. gofmt 是强制的:永远不要手动调格式,交给 gofmt/goimports。
  7. 用 golangci-lint:集成 staticcheck、go vet、unused 等多类静态检查。
  8. 接口定义在消费方:Go 风格是消费者定义接口(interface),而非提供者。
  9. 结构体对齐:字段按大小降序排列可减少 struct 内存占用(对齐填充)。

相关技能


参考资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.