How to override a dependency in go modules?

ぃ、小莉子 提交于 2020-06-13 08:50:10

问题


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"

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.

Thanks!


回答1:


This is what the replace directive is for.

Quoting from wiki Go 1.11 Modules: When should I use the replace directive?

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.

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
)

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.)

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



来源:https://stackoverflow.com/questions/57266010/how-to-override-a-dependency-in-go-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!