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 后的代码

相关推荐
Zenova EdgeOS1 天前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
JavaPub-rodert1 天前
从零搭建通用管理后台:ShiyuAdmin 的 Go、React 与 RBAC 实践
开发语言·react.js·golang
microrain1 天前
SagooIoT 的数据接入层只有四张表,却能接住三种外部数据
物联网·golang·数据建模·sagooiot
fly-iot1 天前
【FDE知识库】(4):企业知识库怎么选型,开源知识库分享几个,选择开源协议友好的版本,开发wiki-go项目
golang·开源·开源协议
福大大架构师每日一题2 天前
excelize v2.11.1发布:Go 1.26.0起步,16项安全修复与嵌入对象能力全面升级
开发语言·安全·golang
小小龙学IT2 天前
Go 语言 pprof 性能剖析与调优深度解析:把“卡在哪“变成一张可执行的数据图
开发语言·后端·golang
十二同学啊2 天前
Go 工程化详解:项目结构、代码规范、测试体系与 CI/CD 实践
ci/cd·golang·代码规范
利威尔xu2 天前
Go IM 源码实战(14):本地缓存快 3000 倍,失效却难 10 倍
后端·缓存·微服务·面试·golang
利威尔xu2 天前
Go IM 源码实战(13):30 个会话只有 2 个红点,未读数错在三条路径
redis·后端·微服务·面试·golang
FungLeo2 天前
成为全栈·Go 后端篇·从 Node SQLite 到 PostgreSQL:把迁移做成独立交付物
postgresql·golang·sqlite·go·数据库迁移·成为全栈