Rich Go language support for Visual Studio Code

Source: Internet
Author: User
Tags install go gocode sourcegraph
This is a creation in Article, where the information may have evolved or changed.


Https://marketplace.visualstudio.com/items/lukehoban.Go


GoLukehoban| 9357 installsRich Go language support for Visual Studio CodeGet Startedinstall*





Go for Visual Studio Code






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


    • Colorization
    • Completion Lists (usinggocode)
    • Snippets
    • Quick Info (usinggodef)
    • Goto Definition (usinggodef)
    • Find References (usinggo-find-references)
    • File outline (usinggo-outline)
    • Rename (usinggorename)
    • Build-on-save (usinggo buildandgo test)
    • Format (usinggoreturnsorgoimportsorgofmt)
    • Add Imports (usinggo list)
    • [partially implemented] Debugging (usingdelve)


IDE Features






Debugger






Using



First, you'll need to install Visual Studio Code0.10. In the Command Palette (cmd-shift-p) Select andInstall ExtensionchooseGo.



In a terminal window with the GOPATH environment variable set to the Gopath you want to work on, launchcode.  Open your Gopath folder or any subfolder you want to work on and then open a.gofile to start editing. You should seeAnalysis Tools Missingin the bottom right, clicking this would offer to install all of the Go tooling needed for the Extensio  N to suppport It's full feature set. See Thetools sections below for more details.



Note: It is the strongly encouraged to turn onAuto SaveVisual Studio Code (File -> Auto Save) when using the this extension. Many of the Go tools work is only on saved files, and error reporting'll is more interactive withAuto Saveturned on.



Options



The following Visual Studio Code settings is available for the Go extension. These can is set in User Preferences (cmd+,) or workspace settings (.vscode/settings.json).


{
    "go.buildOnSave": true,
    "go.lintOnSave": true,
    "go.vetOnSave": true,
    "go.buildFlags": [],
    "go.lintFlags": [],
    "go.vetFlags": [],
    "go.formatOnSave": false,
    "go.formatTool": "goreturns",
    "go.goroot": "/usr/local/go",
    "go.gopath": "/Users/lukeh/go"
}


Commands



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


    • Go: Add ImportTo add a import from the list of pacakges in your Go context
    • Go: Current GOPATHTo see your currently configured Gopath
    • Go: Run test at cursorTo run a test at the current cursor position in the active document
    • Go: Run tests in current packageTo run all tests in the pacakge containing the active document
    • Go: Run tests in current fileTo run all tests in the current active document


Optional: Debugging



To use the debugger, you must currently manually installdelve.  See theinstallation instructions for full details. This isn't yet supported on Windows, and in OS X it requires creating a self-signed cert to sign thedlvbinary.



Once This was installed, go to the Code debug Viewlet and select the configuration gear, placing the following in your Laun Ch.json:


{
    "version": "0.1.0",
    "configurations": [
        {
            "name": "Launch main.go",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": ".",
            "env": {},
            "args": []
        }
    ]
}


programthe option can refer to a package folder to debug, or a file within that folder.



Themodeparameter can is set to:


    • debugTo compile the contents of the program folder and launch under the debugger. [Default]
    • testTo debug tests in the program folder.
    • execTo run a pre-built binary instead of building, the current code in the program folder.


Building and debugging the Extension



You can set up a development environment for debugging the extension during extension development.



First make sure don't have the extension installed in~/.vscode/extensions. Then clone the repo somewhere else on your machine, run andnpm installopen a development instance of Code.


rm -rf ~/.vscode/extensions/lukehoban.Go
cd ~
git clone https://github.com/Microsoft/vscode-go
cd vscode-go
npm install
code .


You can now go to the Debug viewlet and select then hitLaunch Extensionrun (F5).



[Extension Development Host]in the instance, open your Gopath folder.



You can now hit breakpoints and step through the extension.



If you do edits.tsin the extension files, just reload (cmd-r) The instance of Code to load in the[Extension Development Host]new extension  Code. The debugging instance would automatically reattach.



To debug the debugger, see the Debugadapter Readme.



Tools



The extension uses the following tools, installed in the current Gopath.  If any tools is missing, you'll see the ' analysis tools missing ' warning in the bottom right corner of the editor. Clicking it would offer to install the missing tools for you.


    • Gocode:go get -u -v github.com/nsf/gocode
    • Godef:go get -u -v github.com/rogpeppe/godef
    • Golint:go get -u -v github.com/golang/lint/golint
    • Go-find-references:go get -u -v github.com/lukehoban/go-find-references
    • Go-outline:go get -u -v github.com/lukehoban/go-outline
    • Goreturns:go get -u -v sourcegraph.com/sqs/goreturns
    • Gorename:go get -u -v golang.org/x/tools/cmd/gorename


To install them just paste and run:


go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
go get -u -v github.com/golang/lint/golint
go get -u -v github.com/lukehoban/go-find-references
go get -u -v github.com/lukehoban/go-outline
go get -u -v sourcegraph.com/sqs/goreturns
go get -u -v golang.org/x/tools/cmd/gorename


And for debugging:


    • Delve:follow the instructions at https://github.com/derekparker/delve/wiki/Building.


License



MIT






Configuring the Golang development environment in visual Studio code



Author: Chszs



Blog home: Http://blog.csdn.net/chszs



Visual Studio Code is a development tool that I like very much. It would be interesting, then, to develop the Golang program by configuring the Golang development environment on visual Studio code. This article mainly describes the process.



First, the installation of Golang



Golang installation in Windows is relatively simple and the process is omitted.



Verify that:


D:\>go versiongo version go1.5.1 windows/amd64


Note that the installation of Golang to ensure that two environment variables, one is the GOROOT environment variable, and the other is the PATH environment variable to contain the $goroot\bin value.



Second, Golang plug-in introduction



For the Visual Studio Code development tool, there is a good Golang plug-in, its homepage is: https://github.com/microsoft/vscode-go



Features of this plugin include:


    • Code with color
    • Code autocomplete (using Gocode)
    • Code Snippets
    • Quick Tip information (using GODEF)
    • Jump to Definition (using GODEF)
    • Search references (using go-find-references)
    • Document Outline (using Go-outline)
    • Renaming (using Gorename)
    • Save build (using Go build and go test)
    • Code formatting (using Goreturns or Goimports or gofmt)
    • Debug code (using delve)


Third, plug-in installation



1. Install Visual Studio Code version 0.10.2



Go to official website: https://code.visualstudio.com/
Download Visual Studio Code version 0.10.2, the installation process is slightly.



2, install the plug-in



Go to the Visual Studio code interface, use the shortcut key ctrrl+shift+p, open the command panel, select "Extensions:install Extension" and wait for the plugin list to load (this step is slower in the country).
Then in the input box "ext Install" Enter go, select Go 0.6.15, click Install, after the installation is completed requires restart development environment, click the Restart button, automatically refresh the environment.



3. Set GOPATH environment variable



Set the GOPATH environment variable in the Windows system, my value is D:\tools\VSCode\works
Missing Gopath environment variables are usually reported as "$GOPATH not set." Such a mistake.



4. Turn on Auto Save



It is highly recommended that the auto-save function be turned on. To open the method: Select Menu file, click Auto Save.



5. Install git



The process of installing Git in Windows is omitted. Remember to configure the Git\bin in the PATH environment variable.



Iv. Configuring Plugins



The configuration options for Visual Studio code support the settings of the Golang plugin and can be configured through user preferences or workspace settings. You can find it at the menu file->preferences.



1. Click User Settings



Found it


// Specifies the GOPATH to use when no environment variable is set."go.gopath": null,


If the GOPATH environment variable is not set, then this is the setting.



2. Execution of Orders



1) Install Gocode


go get -u -v github.com/nsf/gocode


2) Install Godef


go get -u -v github.com/rogpeppe/godef


3) Install Golint


go get -u -v github.com/golang/lint/golint


4) Install Go-find-references


go get -u -v github.com/lukehoban/go-find-references


5) Install Go-outline


go get -u -v github.com/lukehoban/go-outline


6) Install Goreturns


go get -u -v sourcegraph.com/sqs/goreturns


7) Install Gorename


go get -u -v golang.org/x/tools/cmd/gorename


3. Use



After the completion of the above, began Golang development is very convenient. :





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.