Lint
环境
安装 golangci-lint
1$ go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
1@if "%DEBUG%"=="" @echo off
2@rem ##########################################################################
3@rem
4@rem Lint all go mod in backend and frontend folder
5@rem
6@rem ##########################################################################
7
8@rem Set local scope for the variables with windows NT shell
9if "%OS%"=="Windows_NT" setlocal
10
11setlocal enabledelayedexpansion
12
13@REM 本脚本所在目录
14set BAT_PATH=%~dp0
15@REM 进入code,使在任意目录运行本脚本,行为都正常
16cd /D %BAT_PATH%
17cd ..
18
19@rem 获取当前目录的绝对路径
20for %%A in ("%cd%") do set "CURRENT_DIR=%%~fA"
21
22@REM 遍历目录下的所有go.mod
23for /r "%CURRENT_DIR%" %%A in (go.mod) do (
24 set "MOD_FILE=%%A"
25 set "SUB_DIR=%%~dpA"
26 pushd "!SUB_DIR!"
27
28 if exist "go.mod" (
29 echo !SUB_DIR!
30 @REM 运行go lint
31 golangci-lint -c ../../.golangci.yml run --timeout=10m
32 goimports-reviser -company-prefixes "agilebot.com.cn/app/mts" ./...
33 )
34 popd
35)
36
37pause
常用命令
命令
1$ golangci-lint version 2$ golangci-lint cache status # 查看缓存状态 3$ golangci-lint cache status # 清除缓存
跳过指定聚合器 //nolint:聚合器1,聚合器2…… 如:// nolint:golint,errcheck 可以忽略该行错误检查,建议跳过检查时在下一行给出跳过的原因。
常见错误
返回
error
的方法,要检查error
,不能直接忽略变量命名风格 ``CamelCase
,常量目前取消了此限制,可以
CamelCase,也可以
All_CAPS`1revive: 2 severity: error 3 rules: 4 - name: error-naming 5 - name: error-return 6 - name: errorf 7 - name: import-shadowing 8 - name: var-naming 9 arguments: 10 - ["ID", "IDS", "IP", "URL", "HTTP", "JSON", "XML"] # 默认允许的全大写缩写 11 - [] 12 - - upperCaseConst: true 13 skipPackageNameChecks: true
注意 .golangci.yml 格式需要正确,
golangci-lint
已支持revive
的upperCaseConst
https://github.com/golangci/golangci-lint/discussions/4092#discussioncomment-7038132,注意版本不要太旧。使用
errors
包(github.com/pkg/errors
)的Error
代替fmt
包的Error
内存预分配
无用的赋值
变量名和引入的包有重名
不建议命名返回值
不必要的类型转换
其它问题…
参考:
import分组排序和代码格式化工具
安装 goimports-reviser
1$ go install -v github.com/incu6us/goimports-reviser/v3@latest
可以自动调整import和其他代码行格式,代码洁癖利器
参考: