简体   繁体   中英

How do I import generated SDKs in golang?

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

ie, 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM