名称: go-linter-configuration
描述: "为 Go 项目配置和排错 golangci-lint。处理导入解析问题、类型检查问题,并优化本地和 CI 环境的配置。"
元数据:
{
"openclaw":
{
"emoji": "🔍",
"requires": { "bins": ["go", "golangci-lint"] },
"install":
[
{
"id": "golang",
"kind": "script",
"script": "curl -L https://golang.org/dl/go1.21.5.linux-amd64.tar.gz | tar -C /usr/local -xzf -",
"bins": ["go"],
"label": "安装 Go",
},
{
"id": "golangci",
"kind": "script",
"script": "curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1",
"bins": ["golangci-lint"],
"label": "安装 golangci-lint",
},
],
},
}
为 Go 项目配置和排错 golangci-lint。此技能帮助处理导入解析问题、类型检查问题,并优化本地和 CI 环境的配置。
安装 golangci-lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
或使用官方安装脚本:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1
对整个项目运行 linter:
golangci-lint run ./...
使用特定配置运行:
golangci-lint run --config .golangci.yml ./...
run:
timeout: 5m
tests: false
build-tags: []
linters:
disable-all: true
enable:
- gofmt # 仅进行格式检查
linters-settings:
gofmt:
simplify: true
issues:
exclude-use-default: false
max-issues-per-linter: 50
max-same-issues: 3
output:
format: tab
run:
timeout: 5m
tests: true
build-tags: []
linters:
enable:
- gofmt
- govet
- errcheck
- staticcheck
- unused
- gosimple
- ineffassign
linters-settings:
govet:
enable:
- shadow
errcheck:
check-type-assertions: true
staticcheck:
checks: ["all"]
issues:
exclude-use-default: false
max-issues-per-linter: 50
max-same-issues: 3
output:
format: tab
问题:Linter 报告对导入包的未定义引用。
解决方案:使用最小化配置,设置 disable-all: true,并仅启用基本 linter(如 gofmt)。
问题:CI 环境无法正确解析依赖项。
解决方案:
1. 确保 go.mod 和 go.sum 是最新的。
2. 在 CI 中运行 linter 前,先执行 go mod download。
3. 考虑在 CI 环境中使用更简单的 linter。
问题:Linter 在类型检查阶段失败。
解决方案:
1. 暂时禁用需要类型检查的复杂 linter。
2. 使用 --fast 标志进行更快、强度更低的检查。
3. 验证所有导入是否已正确声明。
适用于 GitHub Actions 工作流:
**名称:** 代码质量
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: 设置 Go 环境
uses: actions/setup-go@v4
with:
go-version: '1.21'
cache: true
- name: 下载依赖
run: go mod download
- name: 安装 golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.59.1
- name: 代码检查
run: golangci-lint run --config .golangci.yml ./...
根据项目需求和 CI 性能要求选择合适的 linter。