跳转至

Go 构建与修复

此命令调用 go-build-resolver 代理,以最小改动增量修复 Go 构建错误。

此命令的作用

  1. 运行诊断:执行 go build、go vet、staticcheck
  2. 解析错误:按文件分组并按严重程度排序
  3. 增量修复:一次修复一个错误
  4. 验证每次修复:每次更改后重新运行构建
  5. 报告摘要:显示已修复内容和剩余问题

何时使用

在以下情况下使用 /go-build: - go build ./... 失败并出现错误 - go vet ./... 报告问题 - golangci-lint run 显示警告 - 模块依赖项损坏 - 拉取更改后构建被破坏

执行的诊断命令

# Primary build check
go build ./...

# Static analysis
go vet ./...

# Extended linting (if available)
staticcheck ./...
golangci-lint run

# Module issues
go mod verify
go mod tidy -v

会话示例

User: /go-build

Agent:
# Go Build Resolution

## Initial Diagnostics

```bash
$ go build ./...
internal/service/user.go:25:15: undefined: UserRepository
internal/handler/api.go:42:9: cannot use x (type string) as type int
internal/handler/api.go:58:2: missing return at end of function

Errors found: 3

Fix 1: Undefined Identifier

File: internal/service/user.go:25 Error: undefined: UserRepository Cause: Missing import

// Added import
import "project/internal/repository"

// Changed
var repo UserRepository
// To
var repo repository.UserRepository
$ go build ./...
# 2 errors remaining

Fix 2: Type Mismatch

File: internal/handler/api.go:42 Error: cannot use x (type string) as type int

// Changed
count := params.Get("count")
// To
countStr := params.Get("count")
count, _ := strconv.Atoi(countStr)
$ go build ./...
# 1 error remaining

Fix 3: Missing Return

File: internal/handler/api.go:58 Error: missing return at end of function

func GetUser(id string) (*User, error) {
    if id == "" {
        return nil, ErrInvalidID
    }
    user := findUser(id)
    // Added missing return
    return user, nil
}
$ go build ./...
# Build successful!

Final Verification

$ go vet ./...
# No issues

$ go test ./...
ok      project/internal/service   0.015s
ok      project/internal/handler   0.023s

Summary

Metric Count
Build errors fixed 3
Vet warnings fixed 0
Files modified 2
Remaining issues 0

Build Status: PASS: SUCCESS ```

常见错误修复

错误 典型修复
undefined: X 添加导入或修正拼写错误
cannot use X as Y 类型转换或修正赋值
missing return 添加返回语句
X does not implement Y 添加缺失的方法
import cycle 重构包
declared but not used 删除或使用变量
cannot find package go get 或 go mod tidy

修复策略

  1. 先处理构建错误 - 代码需要能够编译
  2. 再处理 vet 警告 - 修复可疑构造
  3. 最后处理 lint 警告 - 风格和最佳实践
  4. 一次修复一个 - 验证每次更改
  5. 最小改动 - 不要重构,只修复

停止条件

如果发生以下情况,代理将停止并报告: - 同一错误在 3 次尝试后仍然存在 - 修复引入了更多错误 - 需要架构更改 - 缺少外部依赖项

相关命令

  • /go-test - 构建通过后运行测试
  • /go-review - 审查代码质量
  • /verify - 完整验证循环

相关

  • 代理:agents/go-build-resolver.md
  • 技能:skills/golang-patterns/

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 ev/ECC