1
0

feat: 配置 golangci-lint 静态分析并修复存量违规

- 新增 backend/.golangci.yml 配置 12 个 linter(forbidigo、errorlint、errcheck、staticcheck、revive、gocritic、gosec、bodyclose、noctx、nilerr、goimports、gocyclo)
- 新增 lefthook.yml 配置 pre-commit hook 自动运行 lint
- 修复存量代码违规:errors.Is/As 替换、zap.Error 替换、import 排序、errcheck 修复
- 更新 README 补充编码规范说明
- 归档 backend-code-lint 变更
This commit is contained in:
2026-04-24 13:01:48 +08:00
parent 4c78ab6cc8
commit 4c6b49099d
96 changed files with 1290 additions and 1348 deletions

View File

@@ -1,6 +1,7 @@
package errors
import (
stderrors "errors"
"fmt"
"net/http"
)
@@ -70,22 +71,11 @@ func AsAppError(err error) (*AppError, bool) {
if err == nil {
return nil, false
}
var appErr *AppError
if ok := is(err, &appErr); ok {
return appErr, true
}
return nil, false
}
func is(err error, target interface{}) bool {
// 简单的类型断言
if e, ok := err.(*AppError); ok {
// 直接赋值
switch t := target.(type) {
case **AppError:
*t = e
return true
}
var appErr *AppError
if !stderrors.As(err, &appErr) {
return nil, false
}
return false
return appErr, true
}

View File

@@ -104,7 +104,8 @@ func TestPredefinedErrors(t *testing.T) {
func TestAsAppError(t *testing.T) {
t.Run("nil输入", func(t *testing.T) {
_, ok := AsAppError(nil)
appErr, ok := AsAppError(nil)
assert.Nil(t, appErr)
assert.False(t, ok)
})
@@ -122,7 +123,8 @@ func TestAsAppError(t *testing.T) {
})
t.Run("非AppError类型", func(t *testing.T) {
_, ok := AsAppError(errors.New("普通错误"))
appErr, ok := AsAppError(errors.New("普通错误"))
assert.Nil(t, appErr)
assert.False(t, ok)
})
}

View File

@@ -19,7 +19,7 @@ func TestNew_StdoutOnly(t *testing.T) {
func TestNew_WithFileOutput(t *testing.T) {
dir := filepath.Join(os.TempDir(), "nex-logger-test")
os.MkdirAll(dir, 0755)
require.NoError(t, os.MkdirAll(dir, 0o755))
defer os.RemoveAll(dir)
logger, err := New(Config{
@@ -81,7 +81,7 @@ func TestParseLevel(t *testing.T) {
{"info", true},
{"warn", true},
{"error", true},
{"", true}, // 默认为 info
{"", true}, // 默认为 info
{"invalid", true}, // 默认为 info
}
for _, tt := range tests {

View File

@@ -22,9 +22,9 @@ func newRotateWriter(cfg Config) *lumberjack.Logger {
return &lumberjack.Logger{
Filename: logFilePath(cfg.Path),
MaxSize: maxSize, // MB
MaxSize: maxSize, // MB
MaxBackups: maxBackups,
MaxAge: maxAge, // days
MaxAge: maxAge, // days
Compress: cfg.Compress,
}
}