2

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
rustyx
  • 80,671
  • 25
  • 200
  • 267
Keval Bhogayata
  • 4,422
  • 3
  • 13
  • 36
  • 1
    In your `go.mod` file put `package1 v0.0.1` in `require` block. Later bottom of that file `replace package1 v0.0.1 => ./full_path_of/package1`. Now you should be able to call function in `newmodule` – Jibon Dec 20 '21 at 10:23

2 Answers2

3

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.local.mod and use it when building your project: go build -modfile go.local.mod . (can also add go.local.mod to .gitignore).

rustyx
  • 80,671
  • 25
  • 200
  • 267
  • At least for go 1.20, filename must end with .mod , so let the filename be: go.local.mod, also, to keep the file updated with latest sources when running go mod tidy, use go mod tidy -modfile go.local.mod – Jordan Sheinfeld Mar 28 '23 at 03:52
0

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

Ashutosh Singh
  • 721
  • 3
  • 6
  • This could be a way.. The directories were indeed 2 seperate git repos earlier.. But, now I am supposed to merge those git repos and thats how this question came into picture – Keval Bhogayata Dec 22 '21 at 05:48