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

标签 go go-modules go-build

根据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

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

最佳答案

我认为您所显示的帮助的关键部分是处于模块模式时。仅当您在 GOPATH 之外处理代码或设置了 GO111MODULE=on 环境变量时才会发生这种情况,因此我认为您在本地环境中设置了此设置,但在构建服务器中没有设置。

有关启用模块支持的更多信息如下:

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

关于go - 错误: "build flag -mod=vendor only valid when using modules" when building Go project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662147/

相关文章:

go - "go mod tidy"与 "go build"的行为

go build 不会将本地更改编译到 main

go - base64解码然后json解码: base64. NewDecoder EOF错误和json无效字符错误

go - 完全删除安装了 "go get"的软件包?

go - 在 gcp cloudbuild 中使用 `replace` 指令失败

go - 如何防止 `go build`更新模块的最新版本

Golang安装

go - 从 Golang 映射中读取字符串

types - 在 Go 中初始化自定义 int 类型

go - 错误 : failed post-processing: 820:39: missing ',' in argument list