Below is the structure of my golang code,
├── go.work
├── moda
│ ├── go.mod
│ └── main.go
├── go.mod
└── modb
└── main.go
The problem is I'm only able to build binary only for a single module. For building other modules it is only working after i comment out the other modules in go.work
my go.work is as below,
go 1.19
use (
./moda
.
)
so when i do build as below with the above go.work it works fine,
go build -o test-binary -modfile ./moda/go.mod ./moda
but when i try to build for my other module (which is modb/) like below,
go build -o test-binary1 -modfile ./go.mod ./modb
i'm getting below error,
directory modb is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use .
i tried using go work use
but im still facing the same issue. Can someone help me solve this ?
edit 1:
I have a project that needs multiple go.mod files to separate the dependencies of main and submodules and thats when i got to know about go workspaces which worked perfectly for my use case.
The structure of my project is same as mentioned above,
├── go.work
├── submodule
│ ├── go.mod
│ ├── go.sum
│ └── main.go
└── mainmodule
└── main.go
├── go.mod
├── go.sum
now when building executable for submodule go build works fine but when building executable for mainmodule the imports that are present in submodules/go.mod are also getting added to mainmodule executable. I have verified this with go build -n
too.
To avoid the submodule imports getting added to mainmodule executable i used -modfile
but using it i'm only able to build the executable for the submodule but not for mainmodule.
PS: though i have added the example code in github it is not able to reproduce the same issue (submodule imports getting added to mainmodule) not sure if it is because of the imports i used though