如何覆盖go模块中的依赖项? [英] How to override a dependency in go modules?

查看:142
本文介绍了如何覆盖go模块中的依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dep中,您可以选择覆盖依赖项,并使其指向其他存储库,例如,在以下

In dep you have the option to override a dependency and have it point to a different repo for example in the following https://github.com/kubermatic/glog-logrus library one needs to add the following lines to the Gopkg.toml file:

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

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

Then in the codebase you import "github.com/golang/glog. However, in go modules I don't see such an option? which leads me to think the only solution is to change the import to github.com/kubermatic/glog-logrus.

谢谢!

推荐答案

这是replace指令的作用.

引用Wiki 转到1.11版模块:什么时候应该使用replace指令?

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

The replace directive allows you to supply another import path that might be another module located in VCS (GitHub or elsewhere), or on your local filesystem with a relative or absolute file path. The new import path from the replace directive is used without needing to update the import paths in the actual source code.

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

So add this to the go.mod file of your main module:

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

您还可以指示go工具为您进行此修改:

You can also instruct the go tool to make this edit for you:

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

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

(Use the version you're interested in.)

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

After this when you import github.com/golang/glog, github.com/kubermatic/glog-logrus will be used (without having to change import statements).

这篇关于如何覆盖go模块中的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆