Is it possible to generate code in a module after downloading it?

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

Is it possible to generate code in a module after downloading it?

问题

正如标题所说,是否可能在下载模块依赖之后生成代码(类似于//go:generate)?

具体来说,假设有一个名为example.com/protobuf的仓库,其中包含一些.proto文件和几个用于生成不同语言绑定的.sh脚本,还有一个go.mod文件,以便可以将其作为依赖从Go中使用,如下所示:

module example.com/application

go 1.18

require (
  example.com/protobuf v1.0.0
)

然而,生成的.go文件不包含在这个仓库中,它们必须使用其中一个.sh脚本生成,因此如果尝试这样做,会出现错误,例如module example.com/protobuf@latest found (v1.0.0), but does not contain package example.com/protobuf/foo

是否有一种方法可以解决这个问题,而不必使用git子模块之类的方法?

英文:

As the title says, is it possible to generate code (with something like //go:generate) after a module dependency is downloaded?

Specifically, let's say there's a repo example.com/protobuf containing some .proto files and a few .sh scripts for generating bindings in different languages, plus a go.mod so it can be used as a dependency from go like so:

module example.com/application

go 1.18

require (
  example.com/protobuf v1.0.0
)

However, the generated .go files are not included in this repo, they have to be generated using one of the .sh scripts, so if you try this, you get an error like module example.com/protobuf@latest found (v1.0.0), but does not contain package example.com/protobuf/foo

Is there a way around this without resorting to eg. git submodules?

答案1

得分: 2

不,出于明显的安全原因,这是不可能的。

英文:

No this is not possible for obvious security reasons.

huangapple
  • 本文由 发表于 2022年7月5日 20:58:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/72869852.html
匿名

发表评论

匿名网友

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

确定