1. 程式人生 > >go開發工具goclipse的安裝

go開發工具goclipse的安裝

(1) 安裝Eclipse

建議下載Eclipse時直接下載“Eclipse IDE for Java Developers”的package,而不要下載較小的Installer。因為Installer在安裝Eclipse時還要聯網下載,因為科學上網的原因,聯網下載會非常慢甚至失敗。
Eclipse package下載地址:http://www.eclipse.org/downloads/eclipse-packages/

(2) 安裝GoClipse

可以採取線上安裝的方式或離線的方式安裝。
我採用線上安裝的方式可以安裝成功,如果因為網路的原因,可選離線安裝方式。
GoClipse安裝指南:https://github.com/GoClipse/goclipse/blob/latest/documentation/Installation.md#installation

Start Eclipse, go to Help -> Install New Software...
Click the Add... button, then enter the Update Site URL: http://goclipse.github.io/releases/ in the Location field, click OK.
Select the recently added update site in the Work with: dropdown. Type GoClipse in the filter box. Now the Goclipse feature should appear below.
Select the GoClipse feature, make sure "Contact all update sites during install to find required software" is enabled, and complete the wizard.
Dependencies such as CDT will automatically be added during installation.

(3) 配置GoClipse

  • 在Eclipse中,開啟Windows -> Preferences,找到Go,在Go installation中輸入Go SDK的C:\Go目錄。
    在C:\Go目錄下建立一個子目錄,比如work,在Eclipse GOPATH中輸入C:\Go\work。


下面會在C:\Go\work這個目錄下編譯Go的其它工具。

  • 開啟Go -> Tools。對gocode和godef可以點選Download按鈕來從GitHub直接下載。


但是對guru,直接Download會失敗,因為Download時試圖去從Golang官網下載,而Golang官網已經被拉黑了。
不過因為Golang的程式碼都是託管在GitHub上的,所以我們可以通過下面的地址來下載Go tools的原始碼。

https://github.com/golang/tools
下載後,在C:\Go\work\src目錄下建立golang.org/x的目錄結構,並將上一步git clone的Go tools的tools目錄複製到golang.org/x目錄下。

 git clone https://github.com/golang/tools.git

  • 在C:\Go\work\bin目錄下,在命令列中執行(以GitBash為例)

export GOPATH="C:\Go\work"
go build golang.org/x/tools/cmd/guru

/c/Go/bin/go.exe build golang.org/x/tools/cmd/guru


執行命令成功後guru.exe就會生成在C:\Go\work\bin目錄下。

  • 最後在Eclipse中設定好guru。

  • 在Eclipse中寫第一個Go程式

    在Eclipse中新建一個Go project,再在src下建立一個hello目錄,再在hello目錄下新建一個hello.go的Go file。內容如下所示:


右鍵Run as -> Go Application。

如果出錯“Executable … doesn’t exist“,請檢查hello.go的第一行是否為package main,Go需要從main package作為程式入口,這一點和Java程式是不同的。

參考文章:

使用Eclipse和GoClipse作為Go開發IDE