DEV Community

chandra penugonda
chandra penugonda

Posted on

vscode: multiple Go Projects in a directory(Go 1.18+)

To fix error When opening a directory in VSCode that consists of multiple Go projects:

Error

gopls requires a module at the root of your workspace.
You can work with multiple modules by opening each one as a workspace folder.
Improvements to this workflow will be coming soon (https://github.com/golang/go/issues/32394),
and you can learn more here: https://github.com/golang/go/issues/36899.
Enter fullscreen mode Exit fullscreen mode

From Go 1.18 onwards there is native support for multi-module workspaces. This is done by having a go.work file present in your parent directory.

For a directory structure such as:

/parent
├── lesson-1
│   ├── go.mod
│   ├── hello-world.go
└── lesson-2
    ├── go.mod
    ├── slices.go
Enter fullscreen mode Exit fullscreen mode

Create and populate the file by executing go work:

cd parent
go work init
go work use lesson-1
go work use lesson-2
Enter fullscreen mode Exit fullscreen mode

This will add a go.work file in your parent directory that contains a list of directories you marked for usage:

go 1.18

use (
    ./lesson-1
    ./lesson-2
)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)