When opening a directory in VSCode that consists of multiple Go projects the following error appears:
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.
How can this be fixed?
If CodeTogether is still not connecting correctly through your proxy, you can try disabling the proxy support for extensions by setting http. proxySupport to off . For more information on configuring proxy support in VS Code, go to https://code.visualstudio.com/docs/setup/network#_proxy-server-support.
If your network requires a proxy to access the Internet, set the Visual Studio Code proxy as follows: Open Visual Studio Code, click the settings icon in the lower left corner, and click Settings.
You can open the settings. json file with the Preferences: Open Settings (JSON) command in the Command Palette (Ctrl+Shift+P). Once the file is open in an editor, delete everything between the two curly braces {} , save the file, and VS Code will go back to using the default values.
You probably have more than one go module in your workspace. If that is the case, you can change the go extension settings, in order to allow gopls to look for multiple modules in the workspace. Just add the following to your settings.json
:
"gopls": {
"experimentalWorkspaceModule": true,
}
You can read more about gopls
configuration in the docs: https://github.com/golang/tools/blob/master/gopls/doc/settings.md
To solve this, follow below steps :
step 1 : Open Vscode, and then go to settings.
step 2 : In the search bar , type gopls
step 3 : Just below that you will find settings.json, click on that
step 4 : Paste the below code their "gopls": { "experimentalWorkspaceModule": true, }
step 5 : Save it and restart the Vscode, You are good to go now.
Go 1.18+
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:
$ tree /my/parent/dir
/my/parent/dir
├── project-one
│ ├── go.mod
│ ├── project-one.go
│ └── project-one_test.go
└── project-two
├── go.mod
├── project-two.go
└── project-two_test.go
Create and populate the file by executing go work
:
cd /my/parent/dir
go work init
go work use project-one
go work use project-two
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 (
./project-one
./project-two
)
The setting has changed, now you need to use:
"gopls": {
"build.experimentalWorkspaceModule": true,
}
I had this error because I had not created my modules inside a src
directory. So I had:
$GOPATH
-> bin
-> pkg
-> github.com
-> Module
-> go.mod
and that needed to be:
$GOPATH
-> bin
-> pkg
-> src
-> github.com
-> Module
-> go.mod
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With