go-linting

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Go 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:
LinterPurpose
errcheckEnsure errors are handled
goimportsFormat code and manage imports
reviveCommon style mistakes (modern replacement for golint)
govetAnalyze code for common mistakes
staticcheckVarious static analysis checks
Note:
revive
is the modern, faster successor to the now-deprecated
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
.golangci.yml
in your project root:
yaml
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.yml
文件:
yaml
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

Running

运行方式

bash
undefined
bash
undefined

Install

安装

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

快速参考

TaskCommand/Action
Install golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
Run linters
golangci-lint run
Run on path
golangci-lint run ./pkg/...
Config file
.golangci.yml
in project root
CI integrationRun
golangci-lint run
in pipeline
任务命令/操作
安装golangci-lint
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
运行代码检查
golangci-lint run
在指定路径运行
golangci-lint run ./pkg/...
配置文件项目根目录下的
.golangci.yml
CI集成在流水线中运行
golangci-lint run

Linter Selection Guidelines

检查工具选择指南

When you need...Use
Error handling coverageerrcheck
Import formattinggoimports
Style consistencyrevive
Bug detectiongovet, staticcheck
All of the abovegolangci-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