ci: add linter

This commit is contained in:
2026-01-03 17:57:11 +08:00
parent a6a83f8d9f
commit a8b3a34db7
5 changed files with 65 additions and 8 deletions

View File

@@ -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 }}

44
.golangci.yaml Normal file
View File

@@ -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$

View File

@@ -1,9 +1,19 @@
package hello
import (
"fmt"
"net/http"
)
func HelloHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}
func Handler(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Hello, World!"))
if true {
if true {
if true {
fmt.Println("Deeply nested condition")
}
}
}
}

View File

@@ -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())
}
}
}

View File

@@ -6,6 +6,6 @@ import (
)
func main() {
http.HandleFunc("/hello", hello.HelloHandler)
http.HandleFunc("/hello", hello.Handler)
http.ListenAndServe(":8800", nil)
}