From a8b3a34db7d5f61d71f1d2d458271f81490f3ca1 Mon Sep 17 00:00:00 2001 From: Zpekii Date: Sat, 3 Jan 2026 17:57:11 +0800 Subject: [PATCH] ci: add linter --- .gitea/workflows/deploy.yaml | 5 +++- .golangci.yaml | 44 ++++++++++++++++++++++++++++++++++++ hello/hello.go | 16 ++++++++++--- hello/hello_test.go | 6 ++--- main.go | 2 +- 5 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 .golangci.yaml diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index 347b2a7..342558f 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -6,6 +6,7 @@ jobs: Explore-Gitea-Actions: runs-on: ubuntu-ci container: + image: ubuntu:ci volumes: - data:/var/data - deploy:/var/deploy @@ -15,7 +16,7 @@ jobs: - run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!" - run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}." - name: Check out repository code - uses: actions/checkout@v4 + uses: actions/checkout@v6 with: fetch-depth: 1 - run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner." @@ -26,6 +27,8 @@ jobs: - name: Check go version run: | go version + - name: Run golangci-lint + uses: golangci/golangci-lint-action@v9 - name: Tests run: | cd ${{ gitea.workspace }} diff --git a/.golangci.yaml b/.golangci.yaml new file mode 100644 index 0000000..0045312 --- /dev/null +++ b/.golangci.yaml @@ -0,0 +1,44 @@ +version: "2" +linters: + enable: + # --- 风格与规范 --- + - revive # 替代 golint,检查代码风格 + - misspell # 检查拼写错误 + - goconst # 建议将重复的字符串提取为常量 + - loggercheck # 检查日志使用规范 + + # --- 复杂度与质量 --- + - gocognit # 认知复杂度 (推荐) + - gocyclo # 圈复杂度 + - nestif # 检查 if 嵌套深度 + - funlen # 检查函数长度 + + # --- 性能与 Bug 预防 --- + - bodyclose # 检查 HTTP body 是否关闭 (非常重要) + - noctx # 检查是否忘记传 Context + - prealloc # 建议预分配 slice (性能优化) + - unconvert # 移除不必要的类型转换 + - unparam # 检查未使用的函数参数 + - gocritic # 综合性检查,包含多种检查器 + - makezero # 检查 make 的零值参数 + settings: + nestif: + min-complexity: 4 # 最多三层嵌套 + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/hello/hello.go b/hello/hello.go index daefdc1..f71e7fa 100644 --- a/hello/hello.go +++ b/hello/hello.go @@ -1,9 +1,19 @@ package hello import ( + "fmt" "net/http" ) -func HelloHandler(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("Hello, World!")) -} \ No newline at end of file +func Handler(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write([]byte("Hello, World!")) + + if true { + if true { + if true { + fmt.Println("Deeply nested condition") + + } + } + } +} diff --git a/hello/hello_test.go b/hello/hello_test.go index f00e202..eecee83 100644 --- a/hello/hello_test.go +++ b/hello/hello_test.go @@ -1,16 +1,16 @@ package hello import ( - "testing" "net/http/httptest" + "testing" ) func TestHelloHandler(t *testing.T) { w := httptest.NewRecorder() req := httptest.NewRequest("GET", "/hello", nil) - HelloHandler(w, req) + Handler(w, req) expected := "Hello, World!" if w.Body.String() != expected { t.Errorf("Expected %q but got %q", expected, w.Body.String()) } -} \ No newline at end of file +} diff --git a/main.go b/main.go index 87a5743..b26de2d 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,6 @@ import ( ) func main() { - http.HandleFunc("/hello", hello.HelloHandler) + http.HandleFunc("/hello", hello.Handler) http.ListenAndServe(":8800", nil) } \ No newline at end of file