Golang vscode environment error gopls was not able to find modules in your workspace solution

Table of contents

Error message

analyze

solution

Method 1: Keep the workspace consistent with the project path

Solution 2: Use go work to specify the module included in the workspace

Summarize


Error message

Golang was upgraded from the old version to go1.20.5 and opened vscode, and found that the code could not be automatically completed, and vscode jumped out of an error message

gopls was not able to find modules in your workspace.

When outside of GOPATH, gopls needs to know which modules you are working on.

You can fix this by opening your workspace to a folder inside a Go module, or

by using a go.work file to specify multiple modules.

See the documentation for more information on setting up your workspace

analyze

The above error message is very concise. golang introduced the go mod mechanism from v1.11. In order to improve the go mod mechanism, it also introduced the multi-module workspace mechanism from v1.18 (which mods should be included in your workspace? Inside), that is, the go work mechanism, after upgrading from the previous version to v1.18 and after, your vscode working environment may report an error.

The path of the folder opened by my vscode defaults as its workspace (workspace), and the root directory of this workspace must have a go.mod file. If the root directory does not have this go.mod, it is necessary to specify which subdirectories go mod Incorporating into the work area is a bit convoluted: Summarize the following:

  • Generally, the root directory of the project is used as the workspace. If the parent directory or subdirectory of the project is used as the workspace, you need to use the go work command to indicate which mods are included in your workspace
  • There must be a go.mod in the root directory of the workspace. If there is no go.mod, you can generate one. If you don’t want to generate it under the workspace, you need to use the go work command to indicate which mods under the workspace need to be included in the workspace

 Before solving:

 

solution

Method 1: Keep the workspace consistent with the project path

vscode reopens the directory, directly opens the project folder, uses the project path as the workspace, and vscode directly opens the project path, because there is already a go.mod in the root directory of my project path

gopls will not report an error

vscode opens as shown in the figure:

Solution 2: Use go work to specify the module included in the workspace

I use the parent directory of the project directory as the workspace, and use the go work command to incorporate the required modules into the workspace, as shown in the figure

 vscode opens as shown in the figure:

Summarize

The solution to this small problem is mainly to understand the concept of workspace. A workspace can contain multiple modules. A good habit is that the workspace is consistent with your own project path, and there is go.mod in your project root directory

If the workspace is inconsistent with the project path, you need to use go work to incorporate the module into your own workspace, and the path of go work is the relative path of the workspace

Guess you like

Origin blog.csdn.net/u011285281/article/details/131261615