Go 模块的报错文字差不多,修法却不同。这篇用一个本机造的假 GOPROXY(模块路径 example.test/…)逐个复现 8 个最常见的报错,看每一个到底是 go.mod、go.sum、版本还是仓库的问题,并验证网上常见的“go mod download”“-mod=mod”这些修法到底管不管用。
$ go build .(退出码 1):
main.go:3:16: no required module provides package example.test/lib/greet; to add it:
go get example.test/lib/greet
$ go get example.test/lib@v1.0.0(退出码 0):
go: downloading example.test/lib v1.0.0
go: added example.test/lib v1.0.0
go.mod 现在:
module a1
go 1.21
require example.test/lib v1.0.0 // indirect
go.sum 行数:2
$ go run .(退出码 0):
hi from lib v1.0.0no required module provides package:go get 或 go mod tidy。
$ go build .(退出码 1):
main.go:3:16: missing go.sum entry for module providing package example.test/lib/greet (imported by a2); to add:
go get a2
$ go mod download(退出码 0):
$ go build .(退出码 1):
main.go:3:16: missing go.sum entry for module providing package example.test/lib/greet (imported by a2); to add:
go get a2
$ go mod tidy(退出码 0):
$ go run .(退出码 0):
hi from lib v1.0.0go mod download 修不好,go mod tidy 才行,并且要提交 go.sum。
go.sum 原来有:
example.test/lib v1.0.0 h1:qDTLRhd8…
example.test/lib v1.0.0/go.mod h1:mdP6RFl6…
$ go build .(退出码 1):
go: downloading example.test/lib v1.0.0
verifying example.test/lib@v1.0.0: checksum mismatch
downloaded: h1:qDTLRhd8sz5bwucaWISVEOQScHLUcfkIxUkOiUJYiJ0=
go.sum: h1:AAAAAAd8sz5bwucaWISVEOQScHLUcfkIxUkOiUJYiJ0=
SECURITY ERROR
This download does NOT match an earlier download recorded in go.sum.
The bits may have been replaced on the origin server, or an attacker maySECURITY ERROR:先查内容为什么变了,别直接删 go.sum。
$ go get example.test/moved@v1.0.0(退出码 1):
go: downloading example.test/moved v1.0.0
go: example.test/moved@v1.0.0 requires example.test/moved@v1.0.0: parsing go.mod:
module declares its path as: example.test/other
but was required as: example.test/movedmodule 行与 import 路径要一致。
tidy 之后的 go.mod:
module a5
go 1.21
require example.test/needslib v1.0.0
require example.test/lib v1.1.0 // indirect
$ go build .(退出码 1):
go: updates to go.mod needed; to update it:
go mod tidy
$ go run .(退出码 1):
go: updates to go.mod needed; to update it:
go mod tidy
$ go build -mod=mod .(退出码 0):
-mod=mod 之后 go.mod 里 lib 的版本:require example.test/lib v1.1.0 // indirect
$ go run .(退出码 0):
needslib -> hi from lib v1.1.0版本比依赖要求的低,go mod tidy。
$ go build .(退出码 1):
go: go.mod requires go >= 1.99 (running go 1.27.3; GOTOOLCHAIN=local)
$ go build .(退出码 1):
go: downloading go1.99.0 (darwin/arm64)
go: download go1.99.0 for darwin/arm64: toolchain not available
$ go mod tidy(退出码 1):
go: example.test/future@v1.0.0 requires go >= 1.99 (running go 1.27.3; GOTOOLCHAIN=local)GOTOOLCHAIN 的 local 与 auto 行为不同。
$ go build .(退出码 1):
package a7
imports a7/x from main.go
imports a7/y from x.go
imports a7/x from y.go: import cycle not allowed报错给出完整链,抽出共用包。
GOPROXY 对 example.test/gone/pkg 返回 404(退出码 1):
go: example.test/gone/pkg@latest: module example.test/gone/pkg: reading http://127.0.0.1:<端口>/example.test/gone/pkg/@v/list: 404 Not Found
对 example.test/private/pkg 返回 410(退出码 1):
go: example.test/private/pkg@latest: module example.test/private/pkg: reading http://127.0.0.1:<端口>/example.test/private/pkg/@v/list: 410 Gone
GOPROXY=off(退出码 1):
go: example.test/gone/pkg@latest: module lookup disabled by GOPROXY=off
同样的 404 但设了 GOPRIVATE=example.test/*(退出码 1,改走 direct,example.test 解析不了):
go: example.test/gone/pkg@latest: unrecognized import path "example.test/gone/pkg": https fetch: Get "https://example.test/gone/pkg?go-get=1": dial tcp: lookup example.te私有模块用 GOPRIVATE 绕开代理。