-
Notifications
You must be signed in to change notification settings - Fork 0
Go modules support in Visual Studio Code
This wiki page tracks the status of Go modules support in the Go extension for Visual Studio Code
To get Go module support in VS Code, ensure that you have the latest Go extension and use the new language server gopls from Google by adding the below in your settings
// The only setting you need in version 0.10.0 and above to use gopls
"go.useLanguageServer": true
If you are on version lower than 0.10.0 of this extension, then you will need to install gopls manually and add the below extra setting to use the language server from Google
// All the settings you need in version lower than 0.10.0 in order to tell this extension
// to use `gopls` instead of `go-langserver` as the language server
"go.useLanguageServer": true
"go.alternateTools": {
"go-langserver": "gopls",
},
"go.languageServerExperimentalFeatures": {
"format": true,
"autoComplete": true
}
Remember to update your version of gopls
from time to time using Go: Install/Update Tools
so that you keep getting the improvements that are being made to the language server.
If you don't want to use the language server for any reason, then please know that not all the Go tools that this extension depends on supports Go modules. https://github.com/golang/go/issues/24661 is the issue used by the Go tools team to track the update of Go modules support in various Go tools.
Yes, you can! Please see the section right above this question for details
This is mostly due to the limitation of the tools that power these features which are godef
and gocode
respectively. The Go tools team at Google are working on improving them and also working on a language server which will be the long term solution for all language features.
Please try out the language server as described in the first section of this page. If you don't want to use the language server then,
- For slowness in code completion, log an issue in the gocode repo.
- For slowness in code navigation, log an issue in the godef repo or if you chosen to
gogetdoc
in your settings, then log an issue in the gogetdoc repo
Code navigation and code completion definitely works better when using the language server from Google. So, please give that a try. Check the previous question on this wiki to learn how to enable the language server.
If you are using the language server gopls
, and are on version lower than 0.10.2
of this extension, then please add the below to your settings to get missing imports added when you save your file
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
}
If you are not using the language server,
- This extension uses goreturns tool by default to format your files and auto import missing packages. Since this tool doesn't support modules, the auto import feature on file save no longer works.
- Add the setting
"go.formatTool": "goimports"
and then useGo: Install/Update Tools
to install/updategoimports
as it has recently added support for modules.
The below settings are set by default, so you don't need to add them specifically to get the adding of missing imports and removal of unused imports feature when you save your file while using gopls
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
}
We now officially support gopls
, the language server from Google. Please follow the instructions at the top of this page to use it.
- Module support is now available in the main repo for
godef
and so we no longer rely on its fork. As a result, you will no longer needgodef-gomod
. You will be prompted to update your version ofgodef
though. - Go to definition with sub modules will now work when the main module is opened in VS Code. Note: The second jump from sub-modules still won't work and this is being tracked in https://github.com/Microsoft/vscode-go/issues/2296
-
goimports
now supports formatting with modules. Change yourgo.formatTool
setting togoimports
and useGo: Install/Update Tools
to update it to ensure you have the latest changes.
Apart from what we have already achieved in v0.6.90, the below are available in the current beta version
- Code Coverage
- Clickable links in the test output to files where tests are failing.
-
Go to definition, symbol info on hover and Signature Help
- These features will work as expected when the setting
go.docsTool
is set togogetdoc
. - You will be prompted to update
gogetdoc
- These features will work as expected when the setting
-
Go to definition feature
- If you havent changed the
go.docsTool
setting, you will be prompted to install a fork ofgodef
that we are testing. -
Note: This feature may be slower than usual because the fork depends on
go list
which itself is slower when using modules.
- If you havent changed the
-
Auto-completion.
- You will be prompted to install a fork of
gocode
that we are testing as well as to update thegopkgs
tool. - Currently it only works if the package is already imported and used atleast once.
-
Note: This feature may be slower than usual because the fork depends on
go list
which itself is slower when using modules.
- You will be prompted to install a fork of
-
Go: Add Import
&Go: Browse Packages
commands- These commands will show the appropriate packages from the current module instead of GOPATH
-
Formatting
-
goimports
andgoreturns
do not support Go modules yet. So, to get the formatting feature, add"go.formatTool": "gofmt"
in your settings.
-
To ensure you have the latest Go tools with the Go modules support, and follow the prompts to install/update the required tools.
-
Build : The build on save features as well as the
Go: Build ...
commands -
Test : Running tests via the
Go: Test
commands as well as the "run tests" codelens. -
Generating of tests using the
Go: Generate ...
commands - File Outline: Either via the Outline view in the explorer or using the command Cmd+Shift+O or Ctrl+Shift+O
-
Modify tags on struct fields:
Go: Add tags to struct fields
&Go: Remove tags from struct fields
commands - Code snippets
- Debugging
These are tracked as issues and have the label go-modules