plugins/languages/golang/skills/lint/SKILL.md
Go 静态分析规范——golangci-lint v2 配置(version 2 schema、linters.default、formatters 段、exclusions 预设、depguard 拦截过时包)、必启 linter 集合、与项目规范冲突的禁用项、govulncheck 漏洞扫描、CI 集成。配置 .golangci.yml、修复 lint 告警、做静态质量审查时触发。
npx skillsauth add lazygophers/ccplugin golang-lintInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
golangci-lint 2026-05 当前 v2.12.2,schema 必须 version: "2"。v1 配置需 golangci-lint migrate 一键转换。
.golangci.yml(v2)version: "2"
linters:
default: standard
enable:
# 核心检查
- errcheck
- govet
- ineffassign
- staticcheck
- unused
# 风格 & 复杂度
- cyclop
- goconst
- gocritic
- whitespace
# Bug 预防
- bodyclose
- contextcheck
- copyloopvar # Go 1.22+ 循环变量
- errorlint
- nilerr
- rowserrcheck
- sqlclosecheck
- unparam
# 安全
- gosec
# 测试
- testifylint
- thelper
- paralleltest
# 依赖治理
- depguard
disable:
- wrapcheck # 项目禁包装错误
- err113 # 项目用 sentinel + 错误码
- exhaustruct # 项目允许部分字段初始化
settings:
depguard:
rules:
deprecated-stdlib:
deny:
- pkg: "log"
desc: "use log/slog instead"
- pkg: "math/rand"
desc: "use math/rand/v2 instead"
- pkg: "io/ioutil"
desc: "deprecated since Go 1.16, use os/io"
formatters:
enable:
- gofmt
- goimports
run:
timeout: 5m
tests: true
issues:
max-issues-per-linter: 0
max-same-issues: 0
exclusions:
warn-unused: true
presets:
- common-false-positives
- std-error-handling
rules:
- path: _test\.go
linters:
- gosec
- dupl
- path: internal/api/
linters:
- wrapcheck
output:
formats:
text:
print-linter-name: true
print-issued-lines: true
| Linter | 作用 |
| --- | --- |
| errcheck | 未处理 error |
| govet | Go vet 全集 |
| staticcheck | 静态分析 SA 系列 |
| ineffassign | 无效赋值 |
| unused | 未使用代码 |
| Linter | 作用 |
| --- | --- |
| bodyclose | HTTP body 未关闭 |
| contextcheck | context 链断裂 |
| copyloopvar | Go 1.22+ 循环变量陷阱(旧代码兼容) |
| errorlint | error 比较错误 |
| nilerr | nil error 返回非 nil 值 |
| rowserrcheck | sql.Rows 未检 Err |
| sqlclosecheck | sql 资源未关 |
| gosec | 安全漏洞 |
| testifylint | testify 用法 |
| paralleltest | 缺 t.Parallel |
| depguard | 拦截过时/危险包 |
| Linter | 原因 |
| --- | --- |
| wrapcheck | 项目禁 fmt.Errorf("%w") |
| err113 | 项目用错误码而非动态错误 |
| exhaustruct | 项目允许部分字段初始化 |
go install 编译慢且版本不固定,推荐:
curl -sSfL https://golangci-lint.run/install.sh | sh -s -- -b ./bin v2.12.2
golangci-lint run ./...
golangci-lint run --fix # 自动修复(formatter + 部分 linter)
golangci-lint fmt # 仅跑 formatter
golangci-lint run --fast-only # 快速模式(pre-commit)
golangci-lint linters # 查看启用列表
golangci-lint migrate # v1 → v2 配置迁移
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
govulncheck -mode=binary ./bin/app
每次发布前必跑。
//nolint:errcheck // reason: best-effort write
_ = os.WriteFile(path, data, 0644)
_, _ = os.Open(path) //nolint:errcheck
nolint 必须带 // reason:,无理由滥用视为反模式。
- uses: golangci/golangci-lint-action@v7
with:
version: v2.12.2
args: --timeout=5m
- name: govulncheck
run: |
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
| AI 借口 | 实际应验证 |
| --- | --- |
| "go vet 够了" | 配了 golangci-lint v2? |
| "告警可以忽略" | 全部修复或带理由 nolint? |
| "wrapcheck 很有用" | 与项目规范冲突的禁用了? |
| "CI 不跑 lint" | CI 集成 lint + govulncheck? |
| "depguard 麻烦" | 拦截过时包(log/math/rand/ioutil)? |
| "用 v1 也行" | 升级 v2 + version: "2"? |
.golangci.yml 存在version: "2"linters.default: standard + 显式 enabledepguard 拦截 log/math/rand/io/ioutilparalleltest + testifylint 启用nolint 全部带 reasondevelopment
Go 数据库规范——GORM Model 命名 ModelXxx、表名单数、枚举 uint8 + 常量、索引 idx_ 前缀 + deleted_at leading column、禁 time.Time 统一 int64 unix、禁指针/nullable 字段、TEXT/BLOB/JSON 禁 default、AutoMigrate 禁改主键。设计 DB model、写 GORM tag、建索引、做 migration 审查时触发。
development
Go HTTP API 规范——响应始终 200 + body code 字段、路由 /api/* 全 POST 单段 <Action><Model>、中间件逐路由注册禁 Group(prefix,mw...)、handler 仅返回 (rsp,error)、认证走 header。设计 HTTP API、写路由/handler/中间件时触发。
development
Go 项目结构规范——三层架构(API → Impl → State)、全局状态模式、internal/ 私有包、cmd/ 仅 main.go、go.work 多模块、禁止 Repository 接口和 DI 容器、struct 公共字段开头全 omitempty、handler var rsp 顶声明、禁 legacy migration。设计项目骨架、新建目录、组织包、做架构评审时触发。
development
Go 命名规范——Id/Uid 字段(非 ID)、IsActive/HasMFA 布尔前缀、CreatedAt 时间字段、接收者统一用 p、包名全小写无下划线、泛型类型参数描述性命名、集合字段 xxx_list 禁 xxxs 复数、Enum 0 值 XxxNil 禁 Unknown、禁 Status 统一 State、Set/Update 语义区分。定义结构体字段、函数、变量、包、接收者名、泛型、枚举时触发。