Error: "build flag -mod=vendor only valid when using modules" when building Go project

Error: "build flag -mod=vendor only valid when using modules" when building Go project

根据 this document,我需要将 -mod=vendor 添加到我的构建命令中以使用我的本地 vendor 文件夹:

By default, go commands like go build ignore the vendor directory when in module mode. The -mod=vendor flag (e.g., go build -mod=vendor) instructs the go commands to use the main module's top-level vendor directory to satisfy dependencies.

当我运行这个命令时:

go build -mod=vendor -a -ldflags "-s -w -X github.com/my-api/pkg/config.Version=169.3988801" -o bin/my-api

我收到这个错误:

build flag -mod=vendor only valid when using modules

命令在本地按预期工作,错误仅发生在构建服务器上。

我认为您提供的帮助的关键部分是 when in module mode。只有当你在 GOPATH 之外处理代码或者你设置了 GO111MODULE=on 环境变量时才会发生这种情况,所以我认为你在本地环境中设置了这个而不是构建服务器。

此处提供有关启用模块支持的更多信息:

https://golang.org/cmd/go/#hdr-Preliminary_module_support