Golang lint

命令

golangci-lint run --config=.golangci.yml ./...

Go 复制代码
# 检查当前项目
golangci-lint run --config=.golangci.yml ./...

# 自动修复可修复的问题
golangci-lint run --fix --config=.golangci.yml ./...

# 清理缓存
golangci-lint cache clean

配置文件.golangci.yml

Go 复制代码
linters:
  # Disable all linters.
  # Default: false
  disable-all: true
  # Enable specific linter
  # https://golangci-lint.run/usage/linters/#enabled-by-default
  enable:
    - errcheck
    - gosimple
    - govet
    - ineffassign
    - staticcheck
    - unused
    - gocyclo
    - revive

linters-settings:
  gocyclo:
    # Minimal code complexity to report.
    # Default: 30 (but we recommend 10-20)
    min-complexity: 15
  revive:
    ignore-generated-header: true
    severity: warning
    rules:
      - name: atomic
      - name: blank-imports
      - name: context-as-argument
      - name: context-keys-type
      - name: dot-imports
      - name: error-return
      - name: error-strings
      - name: error-naming
      - name: exported
      - name: increment-decrement
      - name: var-naming
        severity: warning
        disabled: false
        exclude: [""]
        arguments:
          - ["ID","IDS","UID","URL","JSON","API","URL", "IP"]
          - []
          - - skipPackageNameChecks: true
      - name: var-declaration
      - name: package-comments
      - name: range
      - name: receiver-naming
      - name: time-naming
      - name: unexported-return
      - name: indent-error-flow
      - name: errorf
      - name: empty-block
      - name: superfluous-else
      - name: unused-parameter
      - name: unreachable-code
      - name: redefines-builtin-id
  # typecheck:
  #   parallelism: 4
  #   # 设置是否检查内部包和外部包
  #   mode: strict

###配置详解

```

linters:

disable-all: true # 禁用所有默认 linter

enable: # 只启用指定的 linter

  • errcheck

  • gosimple

  • govet

  • ineffassign

  • staticcheck

  • unused

  • gocyclo

  • revive

```

1. 启用的 Linter 说明

Linter 作用 示例 errcheck 检查未处理的错误 f.Close() 未检查返回的 error gosimple 代码简化建议 if x { return true } else { return false } → return x govet Go 静态分析 检测 Printf 格式字符串错误 ineffassign 无效赋值检测 x = 1 后未使用 x staticcheck 综合静态分析 未使用的变量、错误的代码模式 unused 未使用代码检测 未使用的函数、变量、常量 gocyclo 圈复杂度检测 函数过于复杂 revive 代码风格检查 命名规范、注释规范等

2. gocyclo 配置

```

gocyclo:

min-complexity: 15 # 圈复杂度超过 15 报警

```

圈复杂度示例:

```

// 复杂度 = 1 (基础) + if/for/case 分支数

func Example(a, b int) {

if a > 0 { // +1

for i := 0; i < b; i++ { // +1

switch i { // +1

case 1: // +1

case 2: // +1

}

}

}

}

// 总复杂度 = 6

```

3. revive 规则详解

```

revive:

ignore-generated-header: true # 忽略生成文件的头部

severity: warning # 默认严重级别

rules:

``` 关键规则说明

规则 作用 示例 atomic 原子操作检查 atomic.Add 使用正确性 blank-imports 空白导入检查 _ "driver" 是否必要 context-as-argument context 参数位置 context 应为第一个参数 error-return 错误返回检查 错误应作为最后一个返回值 error-naming 错误命名规范 ErrNotFound 而非 ErrorNotFound exported 导出元素注释 大写开头元素需有注释 var-naming 变量命名规范 驼峰命名,特定缩写允许 unused-parameter 未使用参数 参数未使用应改为 _ unreachable-code 不可达代码 return 后的代码

相关推荐
茉莉玫瑰花茶3 小时前
GO [ 标准库 ]
开发语言·后端·golang
紫洋葱hh1 天前
Go 入门基础:类型、变量与运算符
开发语言·后端·ai·golang
codeejun1 天前
每日一Go·MySQL-7、MySQL 核心原理全景图谱
开发语言·mysql·golang
acd120091 天前
Go 里 err 被不经意覆盖的几个现场
开发语言·后端·golang
倔强的小石头_2 天前
从Go+SQLite编译到浏览器开局:landlord部署、AI对局与固定地址实战
人工智能·golang·sqlite
tachibana22 天前
Go 语言的 Map 是否是并发安全的?
开发语言·数据结构·后端·golang·go·语言
后除2 天前
Go 实战: 使用 Wails v2.15 构建轻量级的桌面应用 - 仿微信登录界面
开发语言·微信·golang
看浪的路人2 天前
第8讲:RAG 生产化——Go 实现亿级向量召回 + 混合检索 + 重排序 Pipeline
开发语言·后端·golang
tachibana22 天前
Go 语言 Map 的扩容过程是怎样的?
开发语言·后端·golang·go·语言
念何架构之路2 天前
go-grpc服务端调用链
开发语言·后端·golang