go-linting
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGo Linting
Go代码检查
Source: Uber Go Style Guide
来源:Uber Go风格指南
Core Principle
核心原则
More important than any "blessed" set of linters: lint consistently across a codebase.
Consistent linting helps catch common issues and establishes a high bar for code quality without being unnecessarily prescriptive.
比任何“官方推荐”的代码检查工具集合更重要的是:在整个代码库中保持一致的代码检查标准。
一致的代码检查有助于发现常见问题,并在不过度约束的前提下为代码质量设定高标准。
Minimum Recommended Linters
推荐的基础代码检查工具
Source: Uber Go Style Guide
These linters catch the most common issues while maintaining a high quality bar:
| Linter | Purpose |
|---|---|
| errcheck | Ensure errors are handled |
| goimports | Format code and manage imports |
| revive | Common style mistakes (modern replacement for golint) |
| govet | Analyze code for common mistakes |
| staticcheck | Various static analysis checks |
Note:is the modern, faster successor to the now-deprecatedrevive.golint
来源:Uber Go风格指南
这些工具能发现最常见的问题,同时维持高质量标准:
| 检查工具 | 用途 |
|---|---|
| errcheck | 确保错误被正确处理 |
| goimports | 格式化代码并管理导入语句 |
| revive | 检查常见风格问题(golint的现代替代工具) |
| govet | 分析代码中的常见错误 |
| staticcheck | 各类静态分析检查 |
注意:是现已废弃的revive的现代化、更快的替代工具。golint
Lint Runner: golangci-lint
代码检查运行器:golangci-lint
Source: Uber Go Style Guide
Use golangci-lint as your lint runner:
- Performance: Optimized for large codebases
- Unified config: Configure many linters at once
- Extensible: Add linters as needed for your project
See the example .golangci.yml from uber-go/guide.
来源:Uber Go风格指南
使用golangci-lint作为你的代码检查运行器:
- 性能:针对大型代码库优化
- 统一配置:可同时配置多个检查工具
- 可扩展:可根据项目需要添加检查工具
可参考uber-go/guide中的示例.golangci.yml。
Example Configuration
示例配置
Create in your project root:
.golangci.ymlyaml
linters:
enable:
- errcheck
- goimports
- revive
- govet
- staticcheck
linters-settings:
goimports:
local-prefixes: github.com/your-org/your-repo
revive:
rules:
- name: blank-imports
- name: context-as-argument
- name: error-return
- name: error-strings
- name: exported
run:
timeout: 5m在项目根目录创建文件:
.golangci.ymlyaml
linters:
enable:
- errcheck
- goimports
- revive
- govet
- staticcheck
linters-settings:
goimports:
local-prefixes: github.com/your-org/your-repo
revive:
rules:
- name: blank-imports
- name: context-as-argument
- name: error-return
- name: error-strings
- name: exported
run:
timeout: 5mRunning
运行方式
bash
undefinedbash
undefinedInstall
安装
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
Run all linters
运行所有检查工具
golangci-lint run
golangci-lint run
Run on specific paths
在指定路径运行
golangci-lint run ./pkg/...
---golangci-lint run ./pkg/...
---Quick Reference
快速参考
| Task | Command/Action |
|---|---|
| Install golangci-lint | |
| Run linters | |
| Run on path | |
| Config file | |
| CI integration | Run |
| 任务 | 命令/操作 |
|---|---|
| 安装golangci-lint | |
| 运行代码检查 | |
| 在指定路径运行 | |
| 配置文件 | 项目根目录下的 |
| CI集成 | 在流水线中运行 |
Linter Selection Guidelines
检查工具选择指南
| When you need... | Use |
|---|---|
| Error handling coverage | errcheck |
| Import formatting | goimports |
| Style consistency | revive |
| Bug detection | govet, staticcheck |
| All of the above | golangci-lint with config |
| 当你需要... | 使用工具 |
|---|---|
| 错误处理覆盖检查 | errcheck |
| 导入语句格式化 | goimports |
| 风格一致性检查 | revive |
| 错误检测 | govet, staticcheck |
| 以上全部 | 带配置的golangci-lint |
See Also
参考链接
- For core style principles:
go-style-core - For testing best practices:
go-testing
- 核心风格原则:
go-style-core - 测试最佳实践:
go-testing