问题
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 thereplace
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