How do I import generated SDKs in golang?

703 Views Asked by At

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

There are 1 best solutions below

6
On BEST ANSWER

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.