You need to use replace
keyword in both your “sdk” and “client”. The keyword documentation is here: Modules · golang/go Wiki · GitHub.
By now, you should operate both your client
and sdk
outside of GOPATH
(as in outside $GOPATH/src
). Otherwise, you’re confusing go
that you are not using go.mod
. That means you no longer need to go get
your package module and can work directly with git
in your project structuring.
Example
Assuming you have a workspace outside of GOPATH
, arrange as such:
myProject
+-- sdk
| +-- sdk01
| +-- go.mod
| +-- ...
| +-- sdk02
| +-- go.mod
| +-- ...
+-- client
+-- go.mod
+-- ...
Dealing with SDK
If you’re developing the sdk01
within sdk01
, the sdk01/go.mod
can have the replace keyword as such:
replace url/path/to/sdk01 => ./
// just to show where to add replace
require (
github.com/repo/sdk-name v0.0.0-<hash>
github.com/hashicorp/terraform v0.11.14
)
NOTE:
url/path/to/sdk01
should be the same keyword as the import
path in your source codes, as in:
import "url/path/to/sdk01/pkgName"
Then you should be able to develop the SDK internally without forcing yourself to release it every single time. What happened is that go will always refer to the sdk01/go.mod
location for your sdk1
package.
Dealing with Client
Similarly, for client
, you need to replace all the SDK path pointing to the SDK relatively, with its client/go.mod
replace keyword as such:
replace (
url/path/to/sdk01 => ../sdk/sdk1
url/path/to/sdk02 => ../sdk/sdk2
)
// just to show where to add replace
require (
github.com/repo/sdk-name v0.0.0-<hash>
github.com/hashicorp/terraform v0.11.14
url/path/to/sdk01 v0.0.0
url/path/to/sdk02 v0.0.0
)
Then, your client
will seek out those 2 SDKs locally to build your client binary instead of pulling from the main release URL.
Some Past Learning
There are some important note:
- plan out your release procedures beforehand. It’s easy to release the client’s source codes before the SDK, making your customers confused with dependency error.
- before git-committing with that
replace
clause into any of the go.mod
, please do test on whether will it throws an error if the replacement target is missing or clone directly from main URL. If memory recalls, it shouldn’t throw an error.