【问题标题】:How to override a dependency in go modules?如何覆盖 go 模块中的依赖项?
【发布时间】:2019-07-30 07:01:09
【问题描述】:

dep 中,您可以选择覆盖依赖项并让它指向不同的存储库,例如在以下https://github.com/kubermatic/glog-logrus 库中,需要将以下行添加到 Gopkg.toml 文件中:

[[override]]
  name = "github.com/golang/glog"
  source = "github.com/kubermatic/glog-logrus"

然后在代码库中你import "github.com/golang/glog。但是,在 go 模块中我没有看到这样的选项?这使我认为唯一的解决方案是将导入更改为github.com/kubermatic/glog-logrus

谢谢!

【问题讨论】:

  • 使用“替换”指令。

标签: go dependency-management go-modules


【解决方案1】:

这就是replace 指令的用途。

引用维基Go 1.11 Modules: When should I use the replace directive?

replace 指令允许您提供另一个导入路径,该路径可能是位于 VCS(GitHub 或其他地方)中的另一个模块,或者位于具有相对或绝对文件路径的本地文件系统上。使用来自replace 指令的新导入路径,无需在实际源代码中更新导入路径。

因此,将其添加到主模块的 go.mod 文件中:

replace (
    github.com/golang/glog => github.com/kubermatic/glog-logrus v0.0.0
)

您还可以指示go 工具为您进行此编辑:

go mod edit -replace github.com/golang/glog=github.com/kubermatic/glog-logrus@v0.0.0

(使用您感兴趣的版本。)

之后,当您导入github.com/golang/glog 时,将使用github.com/kubermatic/glog-logrus(无需更改导入语句)。

【讨论】:

  • 我为我的项目中的一个本地模块添加了replace 指令,但是当我在该“替换”模块中进行更改时,这些更改不会在我的项目运行时反映出来。 “替换”指令在哪里查看?实际来源?还是模块的二进制文件?
猜你喜欢
  • 1970-01-01
  • 2014-03-18
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
  • 2019-05-20
相关资源
最近更新 更多