Developing go programs using Visual Studio Code

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

I had tried to develop the Go program with Visual Studio code several times before the release of Visual Studio Code 1.0, but I have given up, and the first is that go plugin is not yet published or imperfect, but vscode is not so convenient, so I have been using Atom + Goplus + godebug Writing go code, but also very handy. Now that Vscode 1.0 was released and supported 10 localized languages, I tried to use it to write go programs with good functionality and performance, so now atom and vscode are my dishes.

This article provides a brief introduction to some of the configuration of the go using Vscode, and a quick overview of the functions of Vscode + go.

Adjust the local language

Vscode will configure the language of your Visual Studio code in accordance with your operating system's localization settings, so it's likely that you'll see it in Simplified Chinese after installation. Currently the Vscode 1.0 supports the following 10 languages:

    • 中文版 (US) en
    • Simplified Chinese ZH-CN
    • Traditional Chinese ZH-TW
    • French FR
    • German de
    • Italian-language it
    • Japanese ja
    • Korean KO
    • Russian RU
    • Spanish ES

I am still accustomed to the English environment, so adjust the localization language to English. If you want to adjust the language, use "CTRL + SHIFT + P" to open the command panel, enter "Conf" will show the "Configure Language" command, Clicking on this command will create a new or open Locale.json file, in which you can set your locale language:

123
{"locale":"en-US"}

I changed it to English display, restart Vscode to show English.

Install the Go plugin

To develop the Go program, you need to install the go plugin. "CTRL + SHIFT + P" to open the naming panel, and then enter "Ext install go" will install the Go plugin, after installation prompts you to restart Vscode, you can write go program.

It supports the following features:

    • Color Highlight Colorization
    • AutoComplete list (using Gocode)
    • Signature Help information for methods and classes (using Godoc)
    • Code Snippets
    • Quick Info (using godef)
    • Lookup definition (using GODEF)
    • Lookup reference (using guru)
    • Document Outline (using Go-outline)
    • Work area symbol search (using Go-symbols)
    • Rename (using Gorename)
    • Compile on save (using go build and go test)
    • Formatting (using Goreturns or Goimports or gofmt)
    • Increased import (using gopkgs)
    • debugging [partial implementations] (using delve)

Its debugging function is commendable, go finally also has a convenient debugging function, plus the breakpoint can be located after the stack information, variables and monitoring custom expressions. (Atom + Godebug can also implement this function, it also through delve debugging)

You can open the Debug window with the fourth icon on the left side of the Vscode toolbar, and clicking on a row makes it easy to add breakpoints to your code.

Of course, the first time you write the go code, you may need to install the tools you need, such as Guru, Go-symbols, Goreturns, and so on. If you set the GOPATH environment variable, start Vscode, open a Go code file, in the lower right corner you will see the "Analysis Tools Missing" prompt, click on it will automatically install the required tools. You can also install them manually:

123456789
Go get-u-v github.com/nsf/gocodego get-u-v github.com/rogpeppe/godefgo get-u-v Github.com/golang/lint/golintgo Get- U-v Github.com/lukehoban/go-outlinego get-u-v sourcegraph.com/sqs/goreturnsgo get-u-v golang.org/x/tools/cmd/ Gorenamego Get-u-V github.com/tpng/gopkgsgo get-u-v github.com/newhook/go-symbolsgo get-u-v golang.org/x/tools/cmd/g Uru

Go plugin settings

Vscode's Go plugin has some customizable configurations that you can configure by opening user preferences or workspace settings. If you want to be valid for all go projects, modify the user preferences.

Click the Menu "File, Preferences, User settting" to open the window, the left side of the window is vscode default configuration, the right window can add our own settings. For the Go plugin, you can add the following settings:

1234567891011121314
{    "Go.buildonsave":true,    "Go.lintonsave":true,    "Go.vetonsave":true,    "Go.buildflags":[],    "Go.lintflags":[],    "Go.vetflags":[],    "Go.coveronsave":false,    "go.usecodesnippetsonfunctionsuggest":false,    "Go.formatonsave":true,    "Go.formattool":"Goreturns",    "Go.goroot":"C:/go",    "Go.gopath":"C:/gopath"}

It customizes some of the behavior and variables of the go plugin. For example, you can change the format tool to "GOFMT", such as "Go.formattool": "Gofmt".

When writing go code, many tools will only be executed when the file is written, such as format, Vet,lint, cover, test, etc., so the timed save file can trigger these actions, so you can add the following configuration in this configuration file:

1
"Files.autosave" "Afterdelay"

It can save files on a timed basis.

Shortcut keys

You can view and define shortcut keys through the menu "File-Preferences-Keyboard shortcuts". The left side of the window shows the default shortcut settings, but not very intuitive, it is recommended to see this link, it is categorized into the function of each shortcut key.

Generally related to the shortcut keys, I will be one of the key special attention, that is, "Ctrl + Space", this key is generally used to set the trigger prompt function, but with the input method switch shortcut keys conflict, so I will redefine it. In the right-hand window that you just opened, add the definition:

12345
{    "key" Ctrl+j ",    "command"Editor.action.triggerSuggest",    "  when "Editortextfocus"}

At the same time I also added a habit of the shortcut keys, is the format Code shortcut point, the default setting is "Shift+alt+f", I added a new shortcut key:

12345
{       "key" Ctrl+alt+f ",       "command"Editor.action.format", "        when"Editortextfocus"   }

and shortcut keys for performing tests

123456789101112
{    "key""Ctrl+alt+t",    "command""Go.test.cursor",    "when"  "Editortextfocus"},{    "key""Ctrl+alt+p",    "command" "Go.test.package",    "when""Editortextfocus"}

The right-hand side of the window has a dialog box to help you add shortcuts, but you can also think of me as a manual addition.

Other functions

The Go plugin and Vscode tools work well together, such as the ability to format code by calling the shortcut key, which does not have to be executed when it is saved. You can also enter "!" in the command panel. View warnings and error messages.

Like sublime text, the command panel is very useful, "CTRL + P" After you open it and enter "?" You can see the different features of it.

Vscode's status bar is also very useful, plug-in information, git information, and document information will be displayed here.

You can change the style of the code highlighting. CTRL + SHIFT + P to open the command panel, enter "theme" to display "Preferences:color theme", click on it to display the available theme. You can also find more theme on Visual Studio marketplace, such as the [Seti Modified] I use, and atom's consistency.

Compared to atom, of course, there are vscode, such as open multiple files, they do not display multiple Windows tab, you also need to "CTRL + TAB" and then select other files. Python is not installed but always hints that Python plugins need to be upgraded and so on. But there are more powerful places than atom, such as "CTRL" + mouse clicks to see the definition of the method, the performance may be better than atom, although on my machine has not seen much performance difference.

This article is just a rough demonstration of the Vscode and go plugin features, I believe that more powerful features have yet to be explored and learned in practice.

Also, it is recommended to read this article on GitHub vs Code Tips and Tricks

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.