跳转至

Go 构建与修复

此命令调用 go-build-resolver 代理,以最少的更改逐步修复 Go build 错误。

此命令的功能

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

使用时机

需要用到 /go-build 时:

  • 当 go build ./... 报错失败时
  • 当 go vet ./... 报告问题时
  • 当 golangci-lint run 显示警告时
  • 当模块依赖损坏时
  • 当 pull 变更后构建失败时

执行的诊断命令

# 기본 build 확인
go build ./...

# 정적 분석
go vet ./...

# 확장 린팅 (사용 가능한 경우)
staticcheck ./...
golangci-lint run

# 모듈 이슈
go mod verify
go mod tidy -v

示例会话

User: /go-build

Agent:
# Go Build 해결

## 초기 진단

```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
```

발견된 에러: 3개

## 수정 1: 정의되지 않은 식별자

파일: internal/service/user.go:25
에러: undefined: UserRepository
원인: import 누락

```go
// import 추가
import "project/internal/repository"

// 변경 전
var repo UserRepository
// 변경 후
var repo repository.UserRepository
```

```bash
$ go build ./...
# 2개 에러 남음
```

## 수정 2: 타입 불일치

파일: internal/handler/api.go:42
에러: cannot use x (type string) as type int

```go
// 변경 전
count := params.Get("count")
// 변경 후
countStr := params.Get("count")
count, _ := strconv.Atoi(countStr)
```

```bash
$ go build ./...
# 1개 에러 남음
```

## 수정 3: 누락된 return

파일: internal/handler/api.go:58
에러: missing return at end of function

```go
func GetUser(id string) (*User, error) {
    if id == "" {
        return nil, ErrInvalidID
    }
    user := findUser(id)
    // 누락된 return 추가
    return user, nil
}
```

```bash
$ go build ./...
# Build 성공!
```

## 최종 검증

```bash
$ go vet ./...
# 이슈 없음

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

## 요약

| 항목 | 수량 |
|------|------|
| 수정된 Build 에러 | 3 |
| 수정된 Vet 경고 | 0 |
| 수정된 파일 | 2 |
| 남은 이슈 | 0 |

Build 상태: PASS: 성공

常见错误

错误 常见的修复方法
undefined: X 添加 import 或修正拼写错误
cannot use X as Y 修正类型转换或赋值
missing return 添加 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