Go extension for Visual Studio Code
Перейти к файлу
Hugo 2e2e3cda2b Add command to fetch import on current line (#1222)
* Add command to fetch import on current line

This will allow users to easily fetch a dependency without having to
take their attention away from the code they're editing.

Microsoft/vscode-go#1189

* Rename goGetImports to goGetImport

The plugin only fetches one import so having the file name plural is
misleading to anybody reading the code.

Microsoft/vscode-go#1189

* Refactor getImportPath

This consolidates the functionality found in two commands into one place
so that if it ever needs to be changed or removed it can be done more
easily.

* Fix lint issues

* Make command title and description more intuitive

Microsoft/vscode-go#1189

* Make name `goGetPackage` consistent

Anybody else who comes along later and reads this is gonna have an
easier time if they only have to keep one name in their head vs. having
to keep two (goGetImport and goGetPackage).

Microsoft/vscode-go#1189

* Exit early if there's no import path to get

If there's no import path to pass to `go get` then there's no point in
spinning up a child process and waiting for it to finish to error out.

This also fixes a bug in `getImportPath` that would cause it to return
invalid input paths for things like comments and function definitions.

Microsoft/vscode-go#1189

* Fallback to selectedText for browsePackage feature

* Offload check for package to go get

`go get` won't download a package if it's already in `$GOPATH/src`.
There's no point in checking for the package to exist and then
downloading with `-u` because `go get -u` and `go get` are effectively
the same when a package hasn't been downloaded yet.

Microsoft/vscode-go#1189

* Use output pane for go get output

`go get -v` gives users more information on what's going on with the
command. This way a user can tell if the package was even downloaded,
because by default `go get` won't output anything.

`go get -v` will output nothing if the package isn't downloaded because
it already exists. To get around this, an information message is shown
saying that the package wasn't downloaded. This keeps the user informed
because they will be expecting feedback from the command.

In the event of an error (like if there's no internet connection), the
error gets shown to the output pane so the user knows what happened.

Microsoft/vscode-go#1189

* Cleanup
2017-09-27 19:05:05 -07:00
.github Use vscode bot for needs more info 2017-09-26 14:17:45 -07:00
.vscode Favor isBackground over deprecated isWatching (#1115) 2017-07-26 23:14:09 -07:00
images code coverage: Added a gutter decorator and bugfix (#1059) 2017-07-10 11:08:58 -06:00
scripts Fix #438 When debug process is stopped kill all processes started by it(#765) 2017-02-06 11:38:24 -08:00
snippets Add helloweb snippet (#1113) 2017-07-26 23:14:41 -07:00
src Add command to fetch import on current line (#1222) 2017-09-27 19:05:05 -07:00
test Add command to fetch import on current line (#1222) 2017-09-27 19:05:05 -07:00
typings Use TextMate snippet syntax supported in latest VSCode API 2017-01-05 10:55:00 -08:00
.editorconfig
.gitignore Prepping for 0.6.63 2017-07-17 12:21:41 -07:00
.travis.yml Show completion for newly created package. (#1191) 2017-09-12 00:01:48 -07:00
.vscodeignore
CHANGELOG.md Update changelog 2017-09-19 10:11:50 -07:00
Go-latest.vsix Dont wait on gopkgs for auto completions 2017-09-26 23:02:42 -07:00
LICENSE
README.md Update README.md 2017-09-20 18:48:57 -07:00
package.json Add command to fetch import on current line (#1222) 2017-09-27 19:05:05 -07:00
thirdpartynotices.txt
tsconfig.json Use TextMate snippet syntax supported in latest VSCode API 2017-01-05 10:55:00 -08:00
tslint.json

README.md

Go for Visual Studio Code

Join the chat at https://gitter.im/Microsoft/vscode-go Build Status

Read the Release Notes to know what has changed over the last few versions of this extension

This extension adds rich language support for the Go language to VS Code, including:

  • Completion Lists (using gocode)
  • Signature Help (using gogetdoc or godef+godoc)
  • Snippets
  • Quick Info (using gogetdoc or godef+godoc)
  • Goto Definition (using gogetdoc or godef+godoc)
  • Find References (using guru)
  • Find implementations (using guru)
  • References CodeLens
  • File outline (using go-outline)
  • Workspace symbol search (using go-symbols)
  • Rename (using gorename. Note: For Undo after rename to work in Windows you need to have diff tool in your path)
  • Build-on-save (using go build and go test)
  • Lint-on-save (using golint or gometalinter)
  • Format on save as well as format manually (using goreturns or goimports or gofmt)
  • Generate unit tests skeleton (using gotests)
  • Add Imports (using gopkgs)
  • Add/Remove Tags on struct fields (using gomodifytags)
  • Semantic/Syntactic error reporting as you type (using gotype-live)
  • Run Tests under the cursor, in current file, in current package, in the whole workspace (using go test)
  • Show code coverage
  • Generate method stubs for interfaces (using impl)
  • [partially implemented] Debugging (using delve)

IDE Features

IDE

How to use this extension?

Install and open Visual Studio Code. Press Ctrl+Shift+X or Cmd+Shift+X to open the Extensions pane. Find and install the Go extension. You can also install the extension from the Marketplace.Open any .go file in VS Code. The extension is now activated.

This extension uses a set of Go tools to provide the various rich features. These tools are installed in your GOPATH by default. If you wish to have these tools in a separate location, provide the desired location in the setting go.toolsGopath. Read more about this and the tools at Go tools that the Go extension depends on

You will see Analysis Tools Missing in the bottom right, clicking this will offer to install all of the dependent Go tools. You can also run the command Go: Install/Update tools to install/update the same.

Note 1: Read GOPATH in the VS Code Go extension to learn about the different ways you can get the extension to set GOPATH.

Note 2: Users may want to consider turning Auto Save on in Visual Studio Code ("files.autoSave": "afterDelay") when using this extension. Many of the Go tools work only on saved files, and error reporting will be more interactive with Auto Save turned on. If you do turn Auto Save on, you may also want to turn format-on-save off ("go.formatOnSave": false), so that it is not triggered while typing.

Note 3: This extension uses gocode to provide completion lists as you type. To provide fresh results, including against not-yet-built dependencies, the extension uses gocode's autobuild=true setting. If you experience any performance issues with autocomplete, you should try setting "go.gocodeAutoBuild": false in your VS Code settings.

Customizing the Go extension features

The Go extension is ready to use on the get go. If you want to customize the features, you can edit the settings in your User or Workspace settings. Read All Settings & Commands in Visual Studio Code Go extension for the full list of options and their descriptions.

Go Language Server (Experimental)

Set go.useLanguageServer to true to use the Go language server from Sourcegraph for features like Hover, Definition, Find All References, Signature Help, Go to Symbol in File and Workspace.

  • This is an experimental feature and is not available in Windows yet.
  • If set to true, you will be prompted to install the Go language server. Once installed, you will have to reload VS Code window. The language server will then be run by the Go extension in the background to provide services needed for the above mentioned features.
  • Everytime you change the value of the setting go.useLanguageServer, you need to reload the VS Code window for it to take effect.
  • To collect traces, set "go.languageServerFlags": ["-trace"]
  • To collect errors from language server in a logfile, set "go.languageServerFlags": ["-trace", "-logfile", "path to a text file that exists" ]

Linter

A linter is a tool giving coding style feedback and suggestions. By default this extension uses the official golint as a linter.

You can change the default linter and use the more advanced Go Meta Linter by setting go.lintTool to "gometalinter" in your settings.

Go meta linter uses a collection of various linters which will be installed for you by the extension.

Some of the very useful linter tools:

  • errcheck checks for unchecked errors in your code.
  • varcheck finds unused global variables and constants.
  • deadcode finds unused code.

If you want to run only specific linters (some linters are slow), you can modify your configuration to specify them:

  "go.lintFlags": ["--disable-all", "--enable=errcheck"],

Alternatively, you can use megacheck which may have significantly better performance than gometalinter, while only supporting a subset of the tools.

Finally, the result of those linters will show right in the code (locations with suggestions will be underlined), as well as in the output pane.

Commands

In addition to integrated editing features, the extension also provides several commands in the Command Palette for working with Go files:

  • Go: Add Import to add an import from the list of packages in your Go context
  • Go: Current GOPATH to see your currently configured GOPATH
  • Go: Test at cursor to run a test at the current cursor position in the active document
  • Go: Test Package to run all tests in the package containing the active document
  • Go: Test File to run all tests in the current active document
  • Go: Test Previous to run the previously run test command
  • Go: Test All Packages in Workspace to run all tests in the current workspace
  • Go: Generates unit tests for package Generates unit tests for the current package
  • Go: Generates unit tests for file Generates unit tests for the current file
  • Go: Generates unit tests for function Generates unit tests for the selected function in the current file
  • Go: Install Tools Installs/updates all the Go tools that the extension depends on
  • Go: Add Tags Adds configured tags to selected struct fields.
  • Go: Remove Tags Removes configured tags from selected struct fields.
  • Go: Generate Interface Stubs Generates method stubs for given interface

You can access all of the above commands from the command pallet (Cmd+Shift+P or Ctrl+Shift+P).

Few of these are available in the editor context menu as an experimental feature as well. To control which of these commands show up in the editor context menu, update the setting go.editorContextMenuCommands

Optional: Debugging

To use the debugger, you must currently manually install delve. See the Installation Instructions for full details. On OS X it requires creating a self-signed cert to sign the dlv binary.

For more read Debugging Go Code Using VS Code

Remote Debugging

To remote debug using VS Code, read Remote Debugging

Building and Debugging the Extension

You can set up a development environment for debugging the extension during extension development. Read more at Building, Debugging and Sideloading the extension in Visual Studio Code

Tools this extension depends on

This extension uses a host of Go tools to provide the various rich features. These tools are installed in your GOPATH by default. If you wish to have the extension use a separate GOPATH for its tools, provide the desired location in the setting go.toolsGopath. Read more about this and the tools at Go tools that the Go extension depends on

License

MIT