Rich Go language support for Visual Studio Code

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

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

                             Go                                                                         lukehoban                                                                                 |                                                                                    9357 installs                                                                                                                       Rich Go language support for Visual Studio CodeGet Started                                                                                 Install*                                                                                                                     

 

Go for Visual Studio Code

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

  • Colorization
  • Completion Lists (using gocode)
  • Snippets
  • Quick Info (using godef)
  • Goto Definition (using godef)
  • Find References (using go-find-references)
  • File outline (using go-outline)
  • Rename (using gorename)
  • Build-on-save (using go build and go test)
  • Format (using goreturns or goimports or gofmt)
  • Add Imports (using go list)
  • [partially implemented] Debugging (using delve)

IDE Features

Debugger

Using

First, you will need to install Visual Studio Code 0.10. In the command palette (cmd-shift-p) selectInstall Extension and chooseGo

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, then open a.go file to start editing.  You should seeAnalysis Tools Missing in the bottom right, clicking this will offer to install all of the Go tooling needed for the extension to suppport it's full feature set.  See theTools section below for more details.

Note: It is strongly encouraged to turn Auto Save on in Visual Studio Code (File -> Auto Save) when using this extension.  Many of the Go tools work only on saved files, and error reporting will be more interactive withAuto Save turned on.

Options

The following Visual Studio Code settings are available for the Go extension.  These can be 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 working with Go files:

  • Go: Add Import to add an import from the list of pacakges in your Go context
  • Go: Current GOPATH to see your currently configured GOPATH
  • Go: Run test at cursor to run a test at the current cursor position in the active document
  • Go: Run tests in current package to run all tests in the pacakge containing the active document
  • Go: Run tests in current file to run all tests in the current active document

Optional: Debugging

To use the debugger, you must currently manually install delve.  See theInstallation Instructions for full details.  This is not yet supported on Windows, and on OS X it requires creating a self-signed cert to sign thedlv binary.

Once this is installed, go to the Code debug viewlet and select the configuration gear, placing the following in your launch.json:

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

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

The mode parameter can be set to:

  • debug to compile the contents of the program folder and launch under the debugger. [default]
  • test to debug tests in the program folder.
  • exec to 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 you do not have the extension installed in ~/.vscode/extensions.  Then clone the repo somewhere else on your machine, runnpm install and open a development instance of Code.

rm -rf ~/.vscode/extensions/lukehoban.Gocd ~git clone https://github.com/Microsoft/vscode-gocd vscode-gonpm installcode .

You can now go to the Debug viewlet and select Launch Extension then hit run (F5).

In the [Extension Development Host] instance, open your GOPATH folder. 

You can now hit breakpoints and step through the extension.

If you make edits in the extension .ts files, just reload (cmd-r) the[Extension Development Host] instance of Code to load in the new extension code.  The debugging instance will 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 are missing, you will see an "Analysis Tools Missing" warning in the bottom right corner of the editor.  Clicking it will 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/gocodego get -u -v github.com/rogpeppe/godefgo get -u -v github.com/golang/lint/golintgo get -u -v github.com/lukehoban/go-find-referencesgo get -u -v github.com/lukehoban/go-outlinego get -u -v sourcegraph.com/sqs/goreturnsgo 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

 

在Visual Studio Code配置GoLang開發環境

作者:chszs
部落客頁:http://blog.csdn.net/chszs

Visual Studio Code是我非常喜歡的開發工具。那麼,如果能在Visual Studio Code上配置GoLang開發環境,以此開發GoLang程式,無疑很有趣。本文主要講述這個過程。

一、GoLang的安裝

GoLang在Windows的安裝比較簡單,過程省略。

驗證一下:

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

要注意,GoLang的安裝要確保兩個環境變數,一個是GOROOT環境變數;二是PATH環境變數要包含$GOROOT\bin值。

二、GoLang外掛程式介紹

對於Visual Studio Code開發工具,有一款優秀的GoLang外掛程式,它的首頁為:https://github.com/microsoft/vscode-go

這款外掛程式的特性包括:

  • 代碼著彩色
  • 代碼自動完成(使用gocode)
  • 程式碼片段
  • 快速提示資訊(使用godef)
  • 跳轉到定義(使用godef)
  • 搜尋參考引用(使用go-find-references)
  • 檔案大綱(使用go-outline)
  • 重新命名(使用gorename)
  • 儲存構建(使用go build和go test)
  • 代碼格式化(使用goreturns或goimports或gofmt)
  • 調試代碼(使用delve)

三、外掛程式安裝

1、安裝Visual Studio Code 0.10.2版

去官方網站:https://code.visualstudio.com/
下載Visual Studio Code 0.10.2版,安裝過程略。

2、安裝外掛程式

進入Visual Studio Code介面,使用快速鍵Ctrrl+Shift+p,開啟命令選擇區,選擇“Extensions: Install Extension”,等待外掛程式名列表的載入(這一步國內較慢)。
然後在輸入框“ext install ”輸入go,選擇go 0.6.15,點擊安裝,安裝完成後要求重啟開發環境,點擊重啟按鈕,自動重新整理環境。

3、設定GOPATH環境變數

在Windows系統中設定GOPATH環境變數,我的值為D:\tools\VSCode\works
缺少GOPATH環境變數通常會報“$GOPATH not set.”這樣的錯誤。

4、開啟自動儲存

強烈建議把自動儲存功能開啟。開啟方法為:選擇菜單File,點擊Auto save。

5、安裝Git

Windows安裝Git的過程省略。記得把git\bin配置到PATH環境變數中。

四、配置外掛程式

Visual Studio Code的配置選項支援GoLang外掛程式的設定,可以通過使用者喜好設定或workspace設定進行配置。在菜單File->Preferences處可以找到。

1、點擊User Settings

找到

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

如果沒有設定GOPATH環境變數,那麼這裡就該設定。

2、執行命令

1)安裝gocode

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

2)安裝godef

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

3)安裝golint

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

4)安裝go-find-references

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

5)安裝go-outline

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

6)安裝goreturns

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

7)安裝gorename

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

3、使用

以上都完成後,開始GoLang開發就很方便了。:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.