645 lines
21 KiB
Markdown
645 lines
21 KiB
Markdown
# 1.4 Go 開發工具
|
||
|
||
本節我將介紹幾個開發工具,它們都具有自動化提示,自動化 fmt 功能。因為它們都是跨平臺的,所以安裝步驟之類別的都是通用的。
|
||
|
||
## LiteIDE
|
||
|
||
LiteIDE 是一款專門為 Go 語言開發的跨平臺輕量級整合開發環境(IDE),由 visualfc 編寫。
|
||
|
||

|
||
|
||
圖 1.4 LiteIDE 主介面
|
||
|
||
**LiteIDE 主要特點:**
|
||
|
||
* 支援主流作業系統
|
||
* Windows
|
||
* Linux
|
||
* MacOS X
|
||
* Go 編譯環境管理和切換
|
||
* 管理和切換多個 Go 編譯環境
|
||
* 支援 Go 語言交叉編譯
|
||
* 與 Go 標準一致的專案管理方式
|
||
* 基於 GOPATH 的套件瀏覽器
|
||
* 基於 GOPATH 的編譯系統
|
||
* 基於 GOPATH 的 Api 文件檢索
|
||
* Go 語言的編輯支援
|
||
* 類別瀏覽器和大綱顯示
|
||
* Gocode(程式碼自動完成工具)的完美支援
|
||
* Go 語言文件檢視和 Api 快速檢索
|
||
* 程式碼表達式資訊顯示`F1`
|
||
* 原始碼定義跳轉支援`F2`
|
||
* Gdb 斷點和除錯支援
|
||
* gofmt 自動格式化支援
|
||
* 其他特徵
|
||
* 支援多國語言介面顯示
|
||
* 完全外掛體系結構
|
||
* 支援編輯器配色方案
|
||
* 基於 Kate 的語法顯示支援
|
||
* 基於全文的單詞自動完成
|
||
* 支援鍵盤快捷鍵繫結方案
|
||
* Markdown 文件編輯支援
|
||
* 即時預覽和同步顯示
|
||
* 自訂 CSS 顯示
|
||
* 可匯出 HTML 和 PDF 文件
|
||
* 批量轉換/合併為 HTML/PDF 文件
|
||
|
||
**LiteIDE 安裝配置**
|
||
|
||
* LiteIDE 安裝
|
||
* 下載地址 <http://sourceforge.net/projects/liteide/files>
|
||
* 原始碼地址 <https://github.com/visualfc/liteide>
|
||
|
||
首先安裝好 Go 語言環境,然後根據作業系統下載 LiteIDE 對應的壓縮檔案直接解壓即可使用。
|
||
|
||
* 編譯環境設定
|
||
|
||
根據自身系統要求切換和配置 LiteIDE 當前使用的環境變數。
|
||
|
||
以 Windows 作業系統,64 位 Go 語言為例,
|
||
工具欄的環境配置中選擇 win64,點 ` 編輯環境`,進入 LiteIDE 編輯 win64.env 檔案
|
||
|
||
GOROOT=c:\go
|
||
GOBIN=
|
||
GOARCH=amd64
|
||
GOOS=windows
|
||
CGO_ENABLED=1
|
||
|
||
PATH=%GOBIN%;%GOROOT%\bin;%PATH%
|
||
。。。
|
||
|
||
將其中的 `GOROOT=c:\go` 修改為當前 Go 安裝路徑,存檔即可,如果有 MinGW64,可以將 `c:\MinGW64\bin` 加入 PATH 中以便 go 呼叫 gcc 支援 CGO 編譯。
|
||
|
||
以 Linux 作業系統,64 位 Go 語言為例,
|
||
工具欄的環境配置中選擇 linux64,點 ` 編輯環境`,進入 LiteIDE 編輯 linux64.env 檔案
|
||
|
||
GOROOT=$HOME/go
|
||
GOBIN=
|
||
GOARCH=amd64
|
||
GOOS=linux
|
||
CGO_ENABLED=1
|
||
|
||
PATH=$GOBIN:$GOROOT/bin:$PATH
|
||
。。。
|
||
|
||
將其中的`GOROOT=$HOME/go`修改為當前 Go 安裝路徑,存檔即可。
|
||
|
||
* GOPATH 設定
|
||
|
||
Go 語言的工具鏈使用 GOPATH 設定,是 Go 語言開發的專案路徑列表,在命令列中輸入(在 LiteIDE 中也可以`Ctrl+,`直接輸入)`go help gopath`快速檢視 GOPATH 文件。
|
||
|
||
在 LiteIDE 中可以方便的檢視和設定 GOPATH。透過 ` 選單-檢視-GOPATH`設定,可以檢視系統中已存在的 GOPATH 列表,
|
||
同時可根據需要新增專案目錄到自訂 GOPATH 列表中。
|
||
|
||
## Sublime Text
|
||
|
||
這裡將介紹 Sublime Text 3(以下簡稱 Sublime)+ GoSublime + gocode 的組合,那麼為什麼選擇這個組合呢?
|
||
|
||
- 自動化提示程式碼,如下圖所示
|
||
|
||

|
||
|
||
圖 1.5 sublime 自動化提示介面
|
||
|
||
- 儲存的時候自動格式化程式碼,讓您編寫的程式碼更加美觀,符合 Go 的標準。
|
||
- 支援專案管理
|
||
|
||

|
||
|
||
圖 1.6 sublime 專案管理介面
|
||
|
||
- 支援語法高亮
|
||
- Sublime Text 3 可免費使用,只是儲存次數達到一定數量之後就會提示是否購買,點選取消繼續用,和正式註冊版本沒有任何區別。
|
||
|
||
|
||
接下來就開始講如何安裝,下載 [Sublime](http://www.sublimetext.com/)
|
||
|
||
根據自己相應的系統下載相應的版本,然後開啟 Sublime,對於不熟悉 Sublime 的同學可以先看一下這篇文章[Sublime Text 全程指南](http://blog.jobbole.com/88648/)或者[sublime text3 入門課程](http://blog.csdn.net/sam976/article/details/52076271)
|
||
|
||
1. 開啟之後安裝 Package Control:Ctrl+` 開啟命令列,執行如下程式碼:
|
||
|
||
適用於 Sublime Text 3:
|
||
|
||
```Go
|
||
import urllib.request,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();urllib.request.install_opener(urllib.request.build_opener(urllib.request.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib.request.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())
|
||
```
|
||
適用於 Sublime Text 2:
|
||
|
||
```Go
|
||
import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp)ifnotos.path.exists(ipp)elseNone;urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler()));open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read());print('Please restart Sublime Text to finish installation')
|
||
```
|
||
|
||
這個時候重啟一下 Sublime,可以發現在在選單欄多了一個如下的節目,說明 Package Control 已經安裝成功了。
|
||
|
||

|
||
|
||
圖 1.7 sublime 套件管理
|
||
|
||
|
||
2. 安裝完之後就可以安裝 Sublime 的外掛了。需安裝 GoSublime、SidebarEnhancements 和 Go Build,安裝外掛之後記得重啟 Sublime 生效,Ctrl+Shift+p 開啟 Package Controll 輸入`pcip`(即“Package Control: Install Package”的縮寫)。
|
||
|
||
這個時候看左下角顯示正在讀取套件資料,完成之後出現如下介面
|
||
|
||

|
||
|
||
圖 1.8 sublime 安裝外掛介面
|
||
|
||
這個時候輸入 GoSublime,按確定就開始安裝了。同理應用於 SidebarEnhancements 和 Go Build。
|
||
|
||
3. 安裝 [gocode](https://github.com/nsf/gocode/)
|
||
|
||
go get -u github.com/nsf/gocode
|
||
|
||
gocode 將會安裝在預設`$GOBIN`
|
||
|
||
另外建議安裝 gotests(產生測試程式碼):
|
||
|
||
|
||
先在 sublime 安裝 gotests 外掛,再執行:
|
||
|
||
|
||
```Go
|
||
go get -u -v github.com/cweill/gotests/...
|
||
```
|
||
|
||
3. 驗證是否安裝成功,你可以開啟 Sublime,開啟 main.go,看看語法是不是高亮了,輸入 `import` 是不是自動化提示了,`import "fmt"`之後,輸入`fmt.`是不是自動化提示有函數了。
|
||
|
||
如果已經出現這個提示,那說明你已經安裝完成了,並且完成了自動提示。
|
||
|
||
如果沒有出現這樣的提示,一般就是你的`$PATH`沒有配置正確。你可以開啟終端,輸入 gocode,是不是能夠正確執行,如果不行就說明`$PATH`沒有配置正確。
|
||
(針對 XP)有時候在終端能執行成功,但 sublime 無提示或者編譯解碼錯誤,請安裝 sublime text3 和 convert utf8 外掛試一試
|
||
|
||
4. MacOS 下已經設定了$GOROOT, $GOPATH, $GOBIN,還是沒有自動提示怎麼辦。
|
||
|
||
請在 sublime 中使用 command + 9, 然後輸入 env 檢查$PATH, GOROOT, $GOPATH, $GOBIN 等變數, 如果沒有請採用下面的方法。
|
||
|
||
首先建立下面的連線, 然後從 Terminal 中直接啟動 sublime
|
||
|
||
|
||
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/sublime
|
||
|
||
|
||
## Visual Studio Code
|
||
|
||
VSCode 是微軟基於 Electron 和 web 技術建構的開源編輯器, 是一款很強大的編輯器。開源地址:https://github.com/Microsoft/VSCode
|
||
|
||
1、安裝 Visual Studio Code 最新版
|
||
|
||
官方網站:https://code.visualstudio.com/
|
||
下載 Visual Studio Code 最新版,安裝過程略。
|
||
|
||
2、安裝 Go 外掛
|
||
|
||
點選右邊的 Extensions 圖示
|
||
搜尋 Go 外掛
|
||
在外掛列表中,選擇 Go,進行安裝,安裝之後,系統會提示重啟 Visual Studio Code。
|
||
|
||
建議把自動儲存功能開啟。開啟方法為:選擇選單 File,點選 Auto save。
|
||
|
||
VSCode 程式碼設定可用於 Go 擴充套件。這些都可以在使用者的喜好來設定或工作區設定(.VSCode/settings.json)。
|
||
|
||
開啟首選項-使用者設定 settings.json:
|
||
|
||
```Go
|
||
|
||
{
|
||
"go.buildOnSave": true,
|
||
"go.lintOnSave": true,
|
||
"go.vetOnSave": true,
|
||
"go.buildFlags": [],
|
||
"go.lintFlags": [],
|
||
"go.vetFlags": [],
|
||
"go.coverOnSave": false,
|
||
"go.useCodeSnippetsOnFunctionSuggest": false,
|
||
"go.formatOnSave": true,
|
||
//goimports
|
||
"go.formatTool": "goreturns",
|
||
"go.goroot": "",//你的 Goroot
|
||
|
||
"go.gopath": "",//你的 Gopath
|
||
|
||
}
|
||
```
|
||
|
||
接著安裝相依套件支援(網路不穩定,請直接到 GitHub [Golang](https://github.com/golang) 下載再移動到相關目錄):
|
||
|
||
```Go
|
||
go get -u -v github.com/nsf/gocode
|
||
go get -u -v github.com/rogpeppe/godef
|
||
go get -u -v github.com/zmb3/gogetdoc
|
||
go get -u -v github.com/golang/lint/golint
|
||
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
|
||
go get -u -v github.com/tpng/gopkgs
|
||
go get -u -v github.com/newhook/go-symbols
|
||
go get -u -v golang.org/x/tools/cmd/guru
|
||
go get -u -v github.com/cweill/gotests/...
|
||
```
|
||
|
||
VSCode 還有一項很強大的功能就是斷點除錯,結合 [delve](https://github.com/derekparker/delve) 可以很好的進行 Go 程式碼除錯
|
||
|
||
```Go
|
||
|
||
go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
|
||
|
||
brew install go-delve/delve/delve(mac 可選)
|
||
```
|
||
|
||
如果有問題再來一遍:
|
||
|
||
```Go
|
||
go get -v -u github.com/peterh/liner github.com/derekparker/delve/cmd/dlv
|
||
```
|
||
注意 : 修改"dlv-cert"證書, 選擇"顯示簡介"->"信任"->"程式碼簽名" 修改為: 始終信任
|
||
|
||
開啟首選項-工作區設定,配置 launch.json:
|
||
|
||
```Go
|
||
{
|
||
"version": "0.2.0",
|
||
"configurations": [
|
||
{
|
||
"name": "main.go",
|
||
"type": "go",
|
||
"request": "launch",
|
||
"mode": "debug",
|
||
"remotePath": "",
|
||
"port": 2345,
|
||
"host": "127.0.0.1",
|
||
"program": "${workspaceRoot}",//工作空間路徑
|
||
"env": {},
|
||
"args": [],
|
||
"showLog": true
|
||
}
|
||
]
|
||
}
|
||
```
|
||
|
||
## Atom
|
||
|
||
Atom 是 GitHub 基於 Electron 和 web 技術建構的開源編輯器, 是一款很漂亮強大的編輯器缺點是速度比較慢。
|
||
|
||
首先要先安裝下 Atom,下載地址: https://atom.io/
|
||
|
||
然後安裝 go-plus 外掛:
|
||
|
||
go-plus 是 Atom 上面的一款開源的 go 語言開發環境的的外掛
|
||
|
||
它需要依賴下面的 go 語言工具:
|
||
|
||
```Go
|
||
1.autocomplete-go :gocode 的程式碼自動提示
|
||
2.gofmt :使用 goftm,goimports,goturns
|
||
3.builder-go:go-install 和 go-test,驗證程式碼,給出建議
|
||
4.gometalinet-linter:goline,vet,gotype 的檢查
|
||
5.navigator-godef:godef
|
||
6.tester-goo :go test
|
||
7.gorename :rename
|
||
```
|
||
|
||
在 Atom 中的 Preference 中可以找到 install 選單,輸入 go-plus,然後點選安裝(install)
|
||
|
||
就會開始安裝 go-plus , go-plus 外掛會自動安裝對應的依賴外掛,如果沒有安裝對應的 go 的類別函式庫會自動執行: go get 安裝。
|
||
|
||
|
||
## GoLand
|
||
|
||
GoLand 是 JetBrains 公司推出的 Go 語言整合開發環境,是 Idea Go 外掛的強化版。GoLand 同樣基於 IntelliJ 平臺開發,支援 JetBrains 的外掛體系。
|
||
|
||
下載地址: https://www.jetbrains.com/go/
|
||
|
||
## Vim
|
||
Vim 是從 vi 發展出來的一個文字編輯器, 程式碼自動完成、編譯及錯誤跳轉等方便程式設計的功能特別豐富,在程式設計師中被廣泛使用。
|
||
|
||
vim-go 是 vim 上面的一款開源的 go 語言使用最為廣泛開發環境的的外掛
|
||
|
||
外掛地址:[github.com/fatih/vim-go](https://github.com/fatih/vim-go)
|
||
|
||
vim 的外掛管理主要有[Pathogen](https://github.com/tpope/vim-pathogen)與[Vundle](https://github.com/VundleVim/Vundle.vim)
|
||
,但是其作用的方面不同。
|
||
pathogen 是為了解決每一個外掛安裝後文件分散到多個目錄不好管理而存在的。vundle 是為了解決自動搜尋及下載外掛而存在的。
|
||
這兩個外掛可同時使用。
|
||
|
||
1.安裝 Vundle
|
||
|
||
|
||
```sh
|
||
mkdir ~/.vim/bundle
|
||
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
|
||
```
|
||
|
||
修改.vimrc,將 Vundle 的相關配置置在最開始處([詳細參考 Vundle 的介紹文件](https://github.com/VundleVim/Vundle.vim))
|
||
|
||
```sh
|
||
set nocompatible " be iMproved, required
|
||
filetype off " required
|
||
|
||
" set the runtime path to include Vundle and initialize
|
||
set rtp+=~/.vim/bundle/Vundle.vim
|
||
call vundle#begin()
|
||
|
||
" let Vundle manage Vundle, required
|
||
Plugin 'gmarik/Vundle.vim'
|
||
|
||
" All of your Plugins must be added before the following line
|
||
call vundle#end() " required
|
||
filetype plugin indent on " required
|
||
```
|
||
|
||
2.安裝 Vim-go
|
||
|
||
修改~/.vimrc,在 vundle#begin 和 vundle#end 間增加一行:
|
||
|
||
```sh
|
||
|
||
Plugin 'fatih/vim-go'
|
||
```
|
||
|
||
在 Vim 內執行: PluginInstall
|
||
|
||
3.安裝 YCM(Your Complete Me)進行自動自動完成
|
||
在~/.vimrc 中新增一行:
|
||
|
||
```sh
|
||
|
||
Plugin 'Valloric/YouCompleteMe'
|
||
```
|
||
在 Vim 內執行: PluginInstall
|
||
|
||
|
||

|
||
|
||
圖 1.9 VIM 編輯器自動化提示 Go 介面
|
||
|
||
接著我們繼續配置 vim:
|
||
|
||
1. 配置 vim 高亮顯示
|
||
|
||
cp -r $GOROOT/misc/vim/* ~/.vim/
|
||
|
||
2. 在~/.vimrc 檔案中增加語法高亮顯示
|
||
|
||
filetype plugin indent on
|
||
syntax on
|
||
|
||
3. 安裝[Gocode](https://github.com/nsf/gocode/)
|
||
|
||
go get -u github.com/nsf/gocode
|
||
|
||
gocode 預設安裝到`$GOPATH/bin`下面。
|
||
|
||
4. 配置[Gocode](https://github.com/nsf/gocode/)
|
||
|
||
~ cd $GOPATH/src/github.com/nsf/gocode/vim
|
||
~ ./update.bash
|
||
~ gocode set propose-builtins true
|
||
propose-builtins true
|
||
~ gocode set lib-path "/home/border/gocode/pkg/linux_amd64"
|
||
lib-path "/home/border/gocode/pkg/linux_amd64"
|
||
~ gocode set
|
||
propose-builtins true
|
||
lib-path "/home/border/gocode/pkg/linux_amd64"
|
||
|
||
>gocode set 裡面的兩個參數的含意說明:
|
||
>
|
||
>propose-builtins:是否自動提示 Go 的內建函式、型別和常數,預設為 false,不提示。
|
||
>
|
||
>lib-path:預設情況下,gocode 只會搜尋**$GOPATH/pkg/$GOOS_$GOARCH** 和 **$GOROOT/pkg/$GOOS_$GOARCH**目錄下的套件,當然這個設定就是可以設定我們額外的 lib 能訪問的路徑
|
||
|
||
|
||
5. 恭喜你,安裝完成,你現在可以使用`:e main.go`體驗一下開發 Go 的樂趣。
|
||
|
||
更多 VIM 設定, 可參考 [ 連結](http://www.cnblogs.com/witcxc/archive/2011/12/28/2304704.html)
|
||
|
||
## Emacs
|
||
Emacs 傳說中的神器,她不僅僅是一個編輯器,它是一個整合環境,或可稱它為整合開發環境,這些功能如讓使用者置身於全功能的作業系統中。
|
||
|
||

|
||
|
||
圖 1.10 Emacs 編輯 Go 主介面
|
||
|
||
1. 配置 Emacs 高亮顯示
|
||
|
||
cp $GOROOT/misc/emacs/* ~/.emacs.d/
|
||
|
||
2. 安裝[Gocode](https://github.com/nsf/gocode/)
|
||
|
||
go get -u github.com/nsf/gocode
|
||
|
||
gocode 預設安裝到`$GOBIN`裡面下面。
|
||
|
||
3. 配置[Gocode](https://github.com/nsf/gocode/)
|
||
|
||
|
||
~ cd $GOPATH/src/github.com/nsf/gocode/emacs
|
||
~ cp go-autocomplete.el ~/.emacs.d/
|
||
~ gocode set propose-builtins true
|
||
propose-builtins true
|
||
~ gocode set lib-path "/home/border/gocode/pkg/linux_amd64" // 換為你自己的路徑
|
||
lib-path "/home/border/gocode/pkg/linux_amd64"
|
||
~ gocode set
|
||
propose-builtins true
|
||
lib-path "/home/border/gocode/pkg/linux_amd64"
|
||
|
||
4. 需要安裝 [Auto Completion](http://www.emacswiki.org/emacs/AutoComplete)
|
||
|
||
下載 AutoComplete 並解壓
|
||
|
||
~ make install DIR=$HOME/.emacs.d/auto-complete
|
||
|
||
配置~/.emacs 檔案
|
||
|
||
;;auto-complete
|
||
(require 'auto-complete-config)
|
||
(add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict")
|
||
(ac-config-default)
|
||
(local-set-key (kbd "M-/") 'semantic-complete-analyze-inline)
|
||
(local-set-key "." 'semantic-complete-self-insert)
|
||
(local-set-key ">" 'semantic-complete-self-insert)
|
||
|
||
詳細資訊參考: http://www.emacswiki.org/emacs/AutoComplete
|
||
|
||
5. 配置.emacs
|
||
|
||
;; golang mode
|
||
(require 'go-mode-load)
|
||
(require 'go-autocomplete)
|
||
;; speedbar
|
||
;; (speedbar 1)
|
||
(speedbar-add-supported-extension ".go")
|
||
(add-hook
|
||
'go-mode-hook
|
||
'(lambda ()
|
||
;; gocode
|
||
(auto-complete-mode 1)
|
||
(setq ac-sources '(ac-source-go))
|
||
;; Imenu & Speedbar
|
||
(setq imenu-generic-expression
|
||
'(("type" "^type *\\([^ \t\n\r\f]*\\)" 1)
|
||
("func" "^func *\\(.*\\) {" 1)))
|
||
(imenu-add-to-menubar "Index")
|
||
;; Outline mode
|
||
(make-local-variable 'outline-regexp)
|
||
(setq outline-regexp "//\\.\\|//[^\r\n\f][^\r\n\f]\\|pack\\|func\\|impo\\|cons\\|var.\\|type\\|\t\t*....")
|
||
(outline-minor-mode 1)
|
||
(local-set-key "\M-a" 'outline-previous-visible-heading)
|
||
(local-set-key "\M-e" 'outline-next-visible-heading)
|
||
;; Menu bar
|
||
(require 'easymenu)
|
||
(defconst go-hooked-menu
|
||
'("Go tools"
|
||
["Go run buffer" go t]
|
||
["Go reformat buffer" go-fmt-buffer t]
|
||
["Go check buffer" go-fix-buffer t]))
|
||
(easy-menu-define
|
||
go-added-menu
|
||
(current-local-map)
|
||
"Go tools"
|
||
go-hooked-menu)
|
||
|
||
;; Other
|
||
(setq show-trailing-whitespace t)
|
||
))
|
||
;; helper function
|
||
(defun go ()
|
||
"run current buffer"
|
||
(interactive)
|
||
(compile (concat "go run " (buffer-file-name))))
|
||
|
||
;; helper function
|
||
(defun go-fmt-buffer ()
|
||
"run gofmt on current buffer"
|
||
(interactive)
|
||
(if buffer-read-only
|
||
(progn
|
||
(ding)
|
||
(message "Buffer is read only"))
|
||
(let ((p (line-number-at-pos))
|
||
(filename (buffer-file-name))
|
||
(old-max-mini-window-height max-mini-window-height))
|
||
(show-all)
|
||
(if (get-buffer "*Go Reformat Errors*")
|
||
(progn
|
||
(delete-windows-on "*Go Reformat Errors*")
|
||
(kill-buffer "*Go Reformat Errors*")))
|
||
(setq max-mini-window-height 1)
|
||
(if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt" "*Go Reformat Output*" nil "*Go Reformat Errors*" t))
|
||
(progn
|
||
(erase-buffer)
|
||
(insert-buffer-substring "*Go Reformat Output*")
|
||
(goto-char (point-min))
|
||
(forward-line (1- p)))
|
||
(with-current-buffer "*Go Reformat Errors*"
|
||
(progn
|
||
(goto-char (point-min))
|
||
(while (re-search-forward "<standard input>" nil t)
|
||
(replace-match filename))
|
||
(goto-char (point-min))
|
||
(compilation-mode))))
|
||
(setq max-mini-window-height old-max-mini-window-height)
|
||
(delete-windows-on "*Go Reformat Output*")
|
||
(kill-buffer "*Go Reformat Output*"))))
|
||
;; helper function
|
||
(defun go-fix-buffer ()
|
||
"run gofix on current buffer"
|
||
(interactive)
|
||
(show-all)
|
||
(shell-command-on-region (point-min) (point-max) "go tool fix -diff"))
|
||
|
||
6. 恭喜你,你現在可以體驗在神器中開發 Go 的樂趣。預設 speedbar 是關閉的,如果開啟需要把 ;; (speedbar 1) 前面的註釋去掉,或者也可以透過 *M-x speedbar* 手動開啟。
|
||
|
||
## Eclipse
|
||
Eclipse 也是非常常用的開發利器,以下介紹如何使用 Eclipse 來編寫 Go 程式。
|
||
|
||

|
||
|
||
圖 1.11 Eclipse 編輯 Go 的主介面
|
||
|
||
1. 首先下載並安裝好[Eclipse](http://www.eclipse.org/)
|
||
|
||
2. 下載[goclipse](https://code.google.com/p/goclipse/)外掛
|
||
|
||
http://code.google.com/p/goclipse/wiki/InstallationInstructions
|
||
|
||
3. 下載 gocode,用於 go 的程式碼自動完成提示
|
||
|
||
gocode 的 GitHub 地址:
|
||
|
||
https://github.com/nsf/gocode
|
||
|
||
在 windows 下要安裝 git,通常用[msysgit](https://code.google.com/p/msysgit/)
|
||
|
||
再在 cmd 下安裝:
|
||
|
||
go get -u github.com/nsf/gocode
|
||
|
||
也可以下載程式碼,直接用 go build 來編譯,會產生 gocode.exe
|
||
|
||
4. 下載[MinGW](http://sourceforge.net/projects/mingw/files/MinGW/)並按要求裝好
|
||
|
||
5. 配置外掛
|
||
|
||
Windows->Reference->Go
|
||
|
||
(1).配置 Go 的編譯器
|
||
|
||

|
||
|
||
圖 1.12 設定 Go 的一些基礎資訊
|
||
|
||
|
||
(2).配置 Gocode(可選,程式碼自動完成),設定 Gocode 路徑為之前產生的 gocode.exe 檔案
|
||
|
||

|
||
|
||
圖 1.13 設定 gocode 資訊
|
||
|
||
(3).配置 GDB(可選,做除錯用),設定 GDB 路徑為 MingW 安裝目錄下的 gdb.exe 檔案
|
||
|
||

|
||
|
||
圖 1.14 設定 GDB 資訊
|
||
|
||
6. 測試是否成功
|
||
|
||
建立一個 go 工程,再建立一個 hello.go。如下圖:
|
||
|
||

|
||
|
||
圖 1.15 建立專案編輯檔案
|
||
|
||
除錯如下(要在 console 中用輸入命令來除錯):
|
||
|
||

|
||
|
||
圖 1.16 除錯 Go 程式
|
||
|
||
## IntelliJ IDEA
|
||
熟悉 Java 的讀者應該對於 idea 不陌生,idea 是透過一個外掛來支援 go 語言的高亮語法,程式碼提示和重構實現。
|
||
|
||
1. 先下載 idea,idea 支援多平臺:win,mac,linux,如果有錢就買個正式版,如果不行就使用社群免費版,對於只是開發 Go 語言來說免費版足夠用了
|
||
|
||

|
||
|
||
2. 安裝 Go 外掛,點選選單 File 中的 Setting,找到 Plugins,點選,Broswer repo 按鈕。國內的使用者可能會報錯,自己解決哈。
|
||
|
||

|
||
|
||
3. 這時候會看見很多外掛,搜尋找到 Golang,雙擊,download and install。等到 golang 那一行後面出現 Downloaded 標誌後,點 OK。
|
||
|
||

|
||
|
||
然後點 Apply .這時候 IDE 會要求你重啟。
|
||
|
||
4. 重啟完畢後,建立新專案會發現已經可以建立 golang 專案了:
|
||
|
||

|
||
|
||
下一步,會要求你輸入 go sdk 的位置,一般都安裝在 C:\Go,linux 和 mac 根據自己的安裝目錄設定,選中目錄確定,就可以了。
|
||
|
||
## links
|
||
* [目錄](<preface.md>)
|
||
* 上一節:[Go 命令](<01.3.md>)
|
||
* 下一節:[總結](<01.5.md>)
|