My folder structure looks something like this... (say my git repo name is demorepo)
demorepo
|--directory1
|-- (no go.mod at this level)
|--module1
|--package1 --------->--------------->--------------------->----|
|--go.mod (github.com/demorepo/directory1/module1) |
|--go.sum |
|--module2 |
|--module3 |
|--directory2 |
|-- (no go.mod at this level) |
|--newmodule ------<------------<------------------<----------------|
Now, I want to use a function defined in "package1" in my "newmodule"
When I hit go get <repo_address>/directort1/module1/package1 at "new_module" it says ....
github.com/<repo>@upgrade found (v0.0.0-20211215055943-92e412ad4a12), but does not contain package github.com/<repo>/directory1/module1/package1
CodePudding user response:
There is a proposal for a Go Workspace File for Go 1.18 which should simplify this task.
Meanwhile, you can use the replace
directive in your go.mod
file to refer to a module located on a local filesystem.
demorepo/directory1/module1/go.mod
:
module github.com/<repo>/directory1/module1
demorepo/directory2/newmodule/go.mod
:
module github.com/<repo>/directory2/newmodule
replace github.com/<repo>/directory1/module1 => ../../directory1/module1
Now you can import github.com/<repo>/directory1/module1/package1
in newmodule
normally and it'll refer to the local module1
.
You might not want the replace
directive in the go.mod
file itself, and instead make a copy of it e.g. go.mod.local
and use it when building your project: go build -modfile go.mod.local .
(can also add *.local
to .gitignore
).
CodePudding user response:
Approach :
If you want to use module1
inside newModule
then you should make one new repo
for module1
put your logic there push it in github
. please make sure you should use appropriate version
for the library.
import
it as a library
and it will work.
Also refer the official docs of module dependency and also check root level dependency.
Official docs : https://go.dev/blog/using-go-modules