Go mod replace(补丁)只替换一个依赖包的方法是:

huangapple go评论84阅读模式
英文:

Go mod replace (patch) only one package of dependency

问题

我在go.mod文件中有一个依赖库,例如foo.bar/dep。我想只修补这个依赖库中的一个包,例如foo.bar/dep/pkg1,并将其放置在源代码库的某个位置,例如./_patch/foo.bar/dep/pkg1

当我尝试这样做并替换它时:

go mod edit -replace foo.bar/dep/pkg1=./_patch/foo.bar/dep/pkg1
go mod tidy

我的go.mod看起来像这样:

require (
  foo.bar/dep v1.0.0
)

replace foo.bar/dep/pkg1 => ./_patch/foo.bar/dep/pkg1

但是当我构建和运行命令时,代码仍然使用原始的foo.bar/dep/pkg1模块,而不是我的修补版本。

是否可能只替换依赖库中的某个特定包,或者唯一的方法是替换整个库?

英文:

I have a dependency library in go.mod file, e.g. foo.bar/dep. I want to patch only one package of this dependency, e.g. foo.bar/dep/pkg1 and place it somewhere in source repository, e.g. ./_patch/foo.bar/dep/pkg1.

When I try to do this and replace it with

go mod edit -replace foo.bar/dep/pkg1=./_patch/foo.bar/dep/pkg1
go mod tidy

and my go.mod looks like

require (
  foo.bar/dep v1.0.0
)

replace foo.bar/dep/pkg1 => ./_patch/foo.bar/dep/pkg1

But when I build and run cmd, the code is still using origin foo.bar/dep/pkg1 module instead of my patch.

Is it possible to replace only some particular package of dependency, or the only way is to replace the whole library?

答案1

得分: 4

> 是否可以仅替换某个特定的依赖包 [?]

不可以。

> [...] 或者唯一的方法是替换整个 [模块]?

是的,这是唯一的方法。

英文:

> Is it possible to replace only some particular package of dependency [?]

No.

> [...] or the only way is to replace the whole [module]?

Yes, this is the only way.

huangapple
  • 本文由 发表于 2022年4月22日 15:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/71965293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定