Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 1 | # Tools |
Hyang-Ah Hana Kim | ac02a87 | 2020-05-13 03:17:52 -0400 | [diff] [blame] | 2 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 3 | This document describes the tools that power the VS Code Go extension. |
Hyang-Ah Hana Kim | 9557e25 | 2020-04-15 18:06:31 -0400 | [diff] [blame] | 4 | |
Jamal Carvalho | 23f22eb | 2022-10-04 15:27:52 +0000 | [diff] [blame] | 5 | Tools will be installed by default when you install the extension. You can also manually install or update all of these tools by running the [`Go: Install/Update Tools`](commands.md#go-installupdate-tools) command. The extension uses pinned versions of command-line tools. See the pinned versions in tools information [here](https://github.com/golang/vscode-go/blob/master/src/goToolsInformation.ts). If any tools are missing, you will see an `Analysis Tools Missing` warning in the bottom-right corner of the editor, which will prompt you to install these tools. |
Hyang-Ah Hana Kim | 9557e25 | 2020-04-15 18:06:31 -0400 | [diff] [blame] | 6 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 7 | VS Code Go will install the tools to your `$GOPATH/bin` by default. |
Hyang-Ah Hana Kim | 9557e25 | 2020-04-15 18:06:31 -0400 | [diff] [blame] | 8 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 9 | ### [`go`] |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 10 | This extension requires you to install the Go toolchain, meaning that you have the `go` command on your [`PATH`](https://en.wikipedia.org/wiki/PATH_(variable)). To do this, follow [the Go installation guide](https://golang.org/doc/install). |
| 11 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 12 | The extension runs the [`go`] command to debug and test your go program. By default, this extension assumes that Go is already installed in your system and the `go` command can be found from the `PATH` (or `Path` in some Windows) environment variable. |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 13 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 14 | This extension works best with the [latest versions of Go](https://golang.org/doc/devel/release.html#policy) but some of the features may continue to work with older versions of Go. The Go release policy states that the two newer major releases are officially supported. |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 15 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 16 | The extension checks for the availability of the new Go release by periodically checking the [official Go distribution site](https://golang.org/dl) and notifies you of the new version. |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 17 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 18 | ### [`gopls`] |
| 19 | [`gopls`] is the official Go [language server](https://langserver.org/) developed by the Go team. It is the default backend for most of this extension's IntelliSense, code navigation, code editing, and diagnostics features. When the extension starts, it spawns a `gopls` instance in server mode for each VS Code project. |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 20 | |
Hana (Hyang-Ah) Kim | 0522373 | 2022-06-14 00:11:17 -0400 | [diff] [blame] | 21 | `gopls` uses the `go` command to analyze your code. The extension automatically propagates necessary settings such as `"go.buildFlags"`, `"go.buildTags"`, `"go.toolsEnvVars"` and the path to the right `go` command to `gopls`. No extra settings should be necessary, but when you need to adjust `gopls`'s behavior further (e.g., enable more advanced analysis features), please see [all the settings for `gopls`](settings.md#settings-for-gopls). |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 22 | |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 23 | If you encounter issues with `gopls`, please read the [troubleshooting guide](troubleshooting.md#collect-gopls-information). If you want to run the extension without the language server, you can disable it by setting `"go.useLanguageServer": false`. |
| 24 | |
| 25 | `gopls` officially supports the four newer major versions of Go. If you are using a very old version of Go, or you explicitly disable the language server, the extension will automatically fall back to the legacy mode. The legacy mode uses old tools instead of `gopls`. Unfortunately many of them are no longer actively maintained and many features the extension provides will not be available. |
Elena Neuschild | 555b11b | 2021-02-06 23:11:42 +0000 | [diff] [blame] | 26 | You can tell whether the extension is using `gopls`, by checking whether the high voltage icon (⚡) is present in the [Go status bar](./ui.md). |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 27 | |
| 28 | `gopls` is under active development, and updating it is important to get new features. The extension periodically checks the [module proxy](https://golang.org/cmd/go/#hdr-Module_proxy_protocol) to detect a new version has been released. When a newer version is available, a pop-up will appear, prompting you to update. If you would like to opt out of this automated update check, set `"go.toolsManagement.checkForUpdates"` to `false`. |
| 29 | For more information about `gopls`, please visit its [documentation](https://golang.org/s/gopls). |
| 30 | |
| 31 | <!-- TODO: link to gopls troubleshooting guide --> |
| 32 | |
| 33 | ### [`dlv`](https://github.com/go-delve/delve) |
| 34 | This extension uses Delve for its debug/test functionalities. The extension currently ships with a thin Debug Adapter that implements the [Debug Adapter protocol](https://microsoft.github.io/debug-adapter-protocol/) and connects VS Code and `dlv`. |
| 35 | |
Elena Neuschild | 555b11b | 2021-02-06 23:11:42 +0000 | [diff] [blame] | 36 | For a comprehensive overview of how to debug your Go programs, please see the [debugging guide](./debugging.md). |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 37 | |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 38 | ### [`goplay`](https://pkg.go.dev/github.com/haya14busa/goplay?tab=overview) |
| 39 | |
| 40 | This tool provides support for the [`Go: Run on Go Playground`](features.md#go-playground) command. |
| 41 | |
| 42 | ### [`gomodifytags`](https://pkg.go.dev/github.com/fatih/gomodifytags?tab=overview) |
| 43 | |
Rebecca Stambler | 4a34f9b | 2020-06-03 20:31:16 -0400 | [diff] [blame] | 44 | This tool provides support for the [`Go: Add Tags to Struct Fields`](features.md#add-or-remove-struct-tags) and [`Go: Remove Tags From Struct Fields`](features.md#add-or-remove-struct-tags) commands. |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 45 | |
| 46 | ### [`impl`](https://github.com/josharian/impl) |
| 47 | |
| 48 | This tool provides support for the [`Go: Generate Interface Stubs`](features.md#generate-interface-implementation) command. |
| 49 | |
| 50 | ### [`gotests`](https://github.com/cweill/gotests/) |
| 51 | |
| 52 | This tool provides support for the [`Go: Generate Unit Tests`](features.md#generate-unit-tests) set of commands. |
| 53 | |
Hana | fd03282 | 2020-12-18 13:01:17 -0500 | [diff] [blame] | 54 | ### [`staticcheck`] |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 55 | |
Hana | fd03282 | 2020-12-18 13:01:17 -0500 | [diff] [blame] | 56 | This is the default lint tool. See the [full list of checks](https://staticcheck.io/docs/checks) that `staticcheck` provides. Other lint tools can be used by configuring the [`"go.lintTool"`](settings.md#go.lintTool) setting. |
| 57 | Other options include: |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 58 | |
Hana | fd03282 | 2020-12-18 13:01:17 -0500 | [diff] [blame] | 59 | * [`golangci-lint`]: This meta-linter combines a number of existing lint tools, including [staticcheck](#staticcheck), into one interface. |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 60 | * [`revive`]: This tool is an enhancement on top of [`golint`], and it provides additional checks. |
Hana | fd03282 | 2020-12-18 13:01:17 -0500 | [diff] [blame] | 61 | * [`golint`]: This used to be the default linter used by this extension before it was officially deprecated. |
Rebecca Stambler | 05e1f73 | 2020-06-08 21:41:18 -0400 | [diff] [blame] | 62 | |
| 63 | You can use the [`"go.lintFlags"`](settings.md#go.lintFlags) setting to further configure your linter of choice. Most linters can be configured via special configuration files, but you may still need to pass these command-line flags. The configuration documentation for each supported linter is listed here: |
| 64 | |
| 65 | * [`staticcheck`](https://staticcheck.io/docs/#configuration) |
| 66 | * [`golangci-lint`](https://golangci-lint.run/usage/configuration/) |
| 67 | * [`revive`](https://github.com/mgechev/revive#command-line-flags) |
| 68 | |
| 69 | #### Examples |
| 70 | |
| 71 | Enable all [`golangci-lint`] linters and only show errors in new code: |
| 72 | |
| 73 | ```json5 |
| 74 | "go.lintFlags": ["--enable-all", "--new"] |
| 75 | ``` |
| 76 | |
| 77 | Configure `revive` to exclude `vendor` directories and apply extra configuration with a `config.toml` file: |
| 78 | |
| 79 | ```json5 |
| 80 | "go.lintFlags": [ |
| 81 | "-exclude=vendor/...", |
| 82 | "-config=${workspaceFolder}/config.toml" |
| 83 | ] |
| 84 | ``` |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 85 | |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 86 | [`goimports`]: https://pkg.go.dev/golang.org/x/tools/cmd/goimports?tab=doc |
| 87 | [`gofmt`]: https://golang.org/cmd/gofmt/ |
Rebecca Stambler | a9811b6 | 2020-05-27 16:55:07 +0000 | [diff] [blame] | 88 | [`golint`]: https://pkg.go.dev/golang.org/x/lint/golint?tab=overview |
| 89 | [`staticcheck`]: https://pkg.go.dev/honnef.co/go/tools/staticcheck?tab=overview |
| 90 | [`golangci-lint`]: https://golangci-lint.run/ |
| 91 | [`revive`]: https://pkg.go.dev/github.com/mgechev/revive?tab=overview |
Hana | f534354 | 2021-01-13 17:28:23 -0500 | [diff] [blame] | 92 | [`gopls`]: https://golang.org/s/gopls |
| 93 | [`go`]: https://golang.org/cmd/go |
| 94 | |