如何在golang中导入生成的SDK?

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

How do I import generated SDKs in golang?

问题

我已经使用openapi-generator分别生成了多个SDK,并将其放置在同一个父目录下:

sdks
--- bar-api
------ go.mod
--- foo-api
------ go.mod

我想在我的客户端代码中导入bar-api。最初,bar-api的go.mod生成如下:

module github.com/coolrepo/bar-api
go 1.13

但是我无法修复客户端代码中的导入:

bar "github.com/cool-repo/bar-api"

所以我做的是:

sdks
--- bar-api
------ go.mod
---go.mod

即,创建了sdks/go.mod

module github.com/coolrepo

go 1.13

并手动编辑了bar-api的go.mod

module github.com/coolrepo/bar-api

require (
	...
)

replace github.com/coolrepo => ./..

go 1.15

有没有更好的修复方法?这个方法似乎可以工作,但是看起来有点不正规,因为使用了replace。

英文:

I've used openapi-generator to generate multiple SDK separately and put it in the same parent dir:

sdks
--- bar-api
------ go.mod
--- foo-api
------ go.mod

I'd like to be able to import bar-api in my client code. Initially there's bar-api's go.mod was generated as:

module github.com/coolrepo/bar-api
go 1.13

but I couldn't fix the imports in my client code:

bar "github.com/cool-repo/bar-api"

so what I did was:

sdks
--- bar-api
------ go.mod
---go.mod

i.e., created sdks/go.mod:

module github.com/coolrepo

go 1.13

and manually edited bar-api's go.mod to:

module github.com/coolrepo/bar-api

require (
	...
)

replace github.com/coolrepo => ./..

go 1.15

Is there a better way to fix it? This one seems to work but looks kinda hacky with this replace.

答案1

得分: 2

“更好的修复方法”是将所有生成的Go包放在一个单独的模块中,而不是将存储库分成多个模块。

  1. rm bar-api/go.mod
  2. go mod tidy

只有在需要能够独立为同一存储库中的一组包标记发布时,才真正需要将存储库拆分为单独的模块。对于生成的服务API,可能不值得为多个模块的版本管理开销而增加开发负担。

英文:

The “better way to fix it” is to put all of the generated Go packages inside a single module, instead of splitting up the repo into multiple modules.

  1. rm bar-api/go.mod
  2. go mod tidy

You only really need to split a repo into separate modules if you need to be able to tag a release for one set of packages independently from another set of packages in the same repo. For generated service APIs, that probably isn't worth the development overhead of juggling versions for multiple modules.

huangapple
  • 本文由 发表于 2021年7月8日 00:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/68290021.html
匿名

发表评论

匿名网友

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

确定