We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
This document describes the features supported by this extension.
Completion results appear for symbols as you type. You can trigger this manually with the Ctrl+Space shortcut.
Autocompletion is also supported for packages you have not yet imported into your program.
Information about the signature of a function pops up as you type in its parameters.
Documentation appears when you hover over a symbol.
Jump to or peek a symbol's declaration.
Find or go to the references of a symbol.
This feature is not available if you are using Go modules without gopls, the Go language server.
gopls
Find the concrete types that implement a given interface.
Search for symbols in your file or workspace by opening the Command Palette (Ctrl+Shift+P) and typing @ for symbols in the current file or # for symbols in the entire workspace.
@
#
Show all calls from or to a function.
See all the symbols in the current file in the VS Code's Outline view.
Quickly toggle between a file and its corresponding test file by using the Go: Toggle Test File command.
Go: Toggle Test File
The default syntax highlighting for Go files is implemented in Visual Studio Code using TextMate grammar, not by this extension.
If you are using gopls, you can enable Semantic Highlighting for more accurate syntax highlighting based on semantic tokenization using this setting:
"gopls": { "ui.semanticTokens": true, / you can optionally turn on these features for more colors / see https://go.dev/issue/45753 and https://go.dev/issue/45792 "ui.noSemanticString": true, / delegates string syntax highlighting to vscode "ui.noSemanticNumber": true, / delegates number syntax highlighting to vscode }
When gopls's semantic tokens feature is enabled, gopls also provides semantic tokens for Go template files (language identifier: gotmpl). By default, the extension associates all *.tmpl or *.gotmpl files in the workspace with gotmpl language. Users can override the language mode by using Visual Studio Code's UI or the "files.associations" setting. See Visual Studio Code's doc for more details.
gotmpl
*.tmpl
*.gotmpl
"files.associations"
Inlay hints render additional inline information to source code to help you understand what the code does. They can be enabled/disabled with the editor.inlayHints.enabled setting in combination with go.inlayHints settings to enable inlay hints types.
editor.inlayHints.enabled
go.inlayHints
i/* int*/, j/* int*/ := 0, len(r)-1
for k/* int*/, v/* string*/ := range []string{} { fmt.Println(k, v) }
{/*in: */"Hello, world", /*want: */"dlrow ,olleH"}
for _, c := range []struct { in, want string }{ /*struct{ in string; want string }*/{"Hello, world", "dlrow ,olleH"}, }
const ( KindNone Kind = iota/* = 0*/ KindPrint/* = 1*/ KindPrintf/* = 2*/ KindErrorf/* = 3*/ )
myFoo/*[int, string]*/(1, "hello")
parseInt(/* str: */ "123", /* radix: */ 8)
Predefined snippets for quick coding. These snippets will appear as completion suggestions when you type. Users can also define their own custom snippets (see Snippets in Visual Studio Code).
Format code and organize imports, either manually or on save.
The extension formats Go code, organizes imports, and removes unused imports by default. For different behavior, please override per-language default settings following the instruction.
When organizing imports, the imported packages are grouped in the default goimports style. In order to group some packages after 3rd-party packages, use the "formatting.local" setting.
goimports
"formatting.local"
"gopls": { "formatting.local": "example.com/myorg,github.com/myorg2" }
The extension organizes imports automatically and can add missing imports if the package is present in your module cache already. However, you can also manually add a new import to your file through the Go: Add Import command. Available packages are offered from module cache (or from your GOPATH in GOPATH mode).
Go: Add Import
GOPATH
In addition to the default go fmt style formatter, the language server (gopls) supports github.com/mvdan/gofumpt style formatting. Use gopls's formatting.gofumpt setting:
go fmt
github.com/mvdan/gofumpt
formatting.gofumpt
"gopls": { "formatting.gofumpt": true }
You can also configure to use other custom formatter (golines) by using the "go.formatTool" setting. The custom formatter must operate on file contents from STDIN, and output the formatted result to STDOUT.
golines
"go.formatTool"
"go.formatTools": "custom", "go.alternateTools": { / the binary name if it is in your PATH, or / provide the exact path to your / custom formatter. "customFormatter": "golines" }
Rename all occurrences of a symbol in your workspace.
Note: For undo after rename to work on Windows, you need to have diff tool on your PATH.
diff
PATH
Select the area for refactoring (e.g. variable, function body, etc). Click on the Code Action light bulb icon that appears in the selected area, or select "Refactoring..." or "Rename Symbol" from the VS Code Context menu. For known issues with this feature see golang/go#37170.
Use the Go: Add Tags to Struct Fields command to automatically generate or remove tags for your struct. This feature is provided by the gomodifytags tool invoked via gopls.
Go: Add Tags to Struct Fields
gomodifytags
Use the Go: Generate Interface Stubs command to automatically generate method stubs for a given interface. This feature is provided by the impl tool.
Go: Generate Interface Stubs
impl
Easily generate unit tests for your project by running one of the Go: Generate Unit Tests for ... commands. This can be done at a function, file, or package level. This feature is provided by the gotests tool.
Go: Generate Unit Tests for ...
gotests
Use the Code Action to automatically fill a struct literal with its default values. The Go language server provides this capability as a refactor.rewrite type code action.
refactor.rewrite
Note: The old "Go: Fill struct" command was removed in v0.40.0 in favor of the Code Action.
The extension, powered by the Go language server (gopls), offers various diagnostics and analyses features, and often with quick fixes to address detected issues.
Compile and type errors are shown as you type by default. This works not only Go source code, but also go.mod, go.work, and Go template files.
go.mod
go.work
The Go language server (gopls) reports analyses settings section.
You can configure an extra linter to run on file save. This behavior is configurable through the "go.lintOnSave" setting.
"go.lintOnSave"
The default lint tool is staticcheck. Popular alternative linters such as revive can be used instead by configuring the "go.lintTool" setting. For a complete overview of linter options, see the documentation for diagnostic tools.
staticcheck
revive
"go.lintTool"
The extension checks the 3rd party dependencies in your code and surfaces vulnerabilities known to the Go vulnerability database. There are two modes that complement each other.
Import-based analysis: this can be enabled using the "go.diagnostic.vulncheck": "Imports" setting. You can turn on and off this analysis conveniently with the "Go: Toggle Vulncheck" command. In this mode, gopls reports vulnerabilities that affect packages directly and indirectly used by your code. The diagnostics are reported in the go.mod file along with quick fixes to help upgrading vulnerable modules.
"go.diagnostic.vulncheck": "Imports"
Govulncheck analysis: this is based on the golang.org/x/vuln/cmd/govulncheck tool, which is embedded in gopls. This provides a low-noise, reliable way to inspect known vulnerabilities. This only surfaces vulnerabilities that actually affect your code, based on which functions in your code are transitively calling vulnerable functions. This can be accessible by the gopls run_govulncheck code lens. The import-based analysis result also provides the "Run govulncheck to verify" option as a quick fix.
Govulncheck
golang.org/x/vuln/cmd/govulncheck
run_govulncheck
"Run govulncheck to verify"
These features require gopls v0.11.0 or newer.
Please share your feedback at https://go.dev/s/vsc-vulncheck-feedback. Report a bug and feature request in our issue tracker.
Notes and Caveats
replace
exclude
"Reset go.mod diagnostics"
To run your code without debugging, use the keyboard shortcut Ctrl+F5 or run the command Debug: Start without Debugging. To debug, see Debugging below.
Ctrl+F5
Debug: Start without Debugging
This command requires you to have a launch configuration in a launch.json file. To open or create your launch.json, run the Debug: Open launch.json command. Use the default Go: Launch file configuration.
launch.json
Debug: Open launch.json
Go: Launch file
Behind the scenes, the Debug: Start without Debugging command calls go run. go run usually requires the path to the file to run, so your launch.json should contain "program": "${file}".
go run
"program": "${file}"
Code lenses allow users to easily run tests, benchmarks, and profiles for a given function, file, package, or workspace.
Alternatively, the same functionality is available through a set of commands: Go: Test Function At Cursor, Go: Test File, Go: Test Package, and Go: Test All Packages in Workspace.
Go: Test Function At Cursor
Go: Test File
Go: Test Package
Go: Test All Packages in Workspace
Show code coverage in the editor, either after running a test or on-demand. This can be done via the commands: Go: Apply Cover Profile and Go: Toggle Test Coverage in Current Package.
Go: Apply Cover Profile
Go: Toggle Test Coverage in Current Package
"Go Test: Profile" menu in Test UI collects CPU/Memory/Mutex profiles and allows visualizing them using pprof (go tool pprof).
go tool pprof
This extension offers debugging of Go programs. See the debugging documentation for more information.
Export your current file to the Go Playground via the Go: Run On Go Playground command. This is useful for quickly creating a piece of sample code.
Go: Run On Go Playground
✏️ Want to contribute to this wiki?
Update the source and send a PR.