Merging other languages
This commit is contained in:
829
en/01.4.md
829
en/01.4.md
@@ -1,430 +1,399 @@
|
||||
# 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 2(以下简称Sublime)+GoSublime的组合,那么为什么选择这个组合呢?
|
||||
|
||||
- 自动化提示代码,如下图所示
|
||||
|
||||

|
||||
|
||||
图1.5 sublime自动化提示界面
|
||||
|
||||
- 保存的时候自动格式化代码,让您编写的代码更加美观,符合Go的标准。
|
||||
- 支持项目管理
|
||||
|
||||

|
||||
|
||||
图1.6 sublime项目管理界面
|
||||
|
||||
- 支持语法高亮
|
||||
- Sublime Text 2可免费使用,只是保存次数达到一定数量之后就会提示是否购买,点击取消继续用,和正式注册版本没有任何区别。
|
||||
|
||||
|
||||
接下来就开始讲如何安装,下载[Sublime](http://www.sublimetext.com/)
|
||||
|
||||
根据自己相应的系统下载相应的版本,然后打开Sublime,对于不熟悉Sublime的同学可以先看一下这篇文章[Sublime Text 2 入门及技巧](http://lucifr.com/139225/sublime-text-2-tricks-and-tips/)
|
||||
|
||||
1. 打开之后安装 Package Control:Ctrl+` 打开命令行,执行如下代码:
|
||||
|
||||
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; 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. 验证是否安装成功,你可以打开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
|
||||
|
||||
|
||||
## Vim
|
||||
Vim是从vi发展出来的一个文本编辑器, 代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。
|
||||
|
||||

|
||||
|
||||
图1.9 VIM编辑器自动化提示Go界面
|
||||
|
||||
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://monnand.me/p/vim-golang-environment/zhCN/)
|
||||
|
||||
## 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>)
|
||||
# Go development tools
|
||||
|
||||
In this section, I'm going to show you a few IDEs that can help you become a more efficient programmer, with capabilities such as intelligent code completion and auto-formatting. They are all cross-platform, so the steps I will be showing you should not be very different, even if you are not using the same operating system.
|
||||
|
||||
## LiteIDE
|
||||
|
||||
LiteIDE is an open source, lightweight IDE for developing Go projects only, developed by visualfc.
|
||||
|
||||

|
||||
|
||||
Figure 1.4 Main panel of LiteIDE
|
||||
|
||||
LiteIDE features.
|
||||
|
||||
- Cross-platform
|
||||
- Windows
|
||||
- Linux
|
||||
- Mac OS
|
||||
- Cross-compile
|
||||
- Manage multiple compile environments
|
||||
- Supports cross-compilation of Go
|
||||
- Project management standard
|
||||
- Documentation view based on $GOPATH
|
||||
- Compilation system based on $GOPATH
|
||||
- API documentation index based on $GOPATH
|
||||
- Go source code editor
|
||||
- Code outlining
|
||||
- Full support of gocode
|
||||
- Go documentation view and API index
|
||||
- View code expression using `F1`
|
||||
- Function declaration jump using `F2`
|
||||
- Gdb support
|
||||
- Auto-format with `gofmt`
|
||||
- Others
|
||||
- Multi-language
|
||||
- Plugin system
|
||||
- Text editor themes
|
||||
- Syntax support based on Kate
|
||||
- intelligent completion based on full-text
|
||||
- Customized shortcuts
|
||||
- Markdown support
|
||||
- Real-time preview
|
||||
- Customized CSS
|
||||
- Export HTML and PDF
|
||||
- Convert and merge to HTML and PDF
|
||||
|
||||
### LiteIDE installation
|
||||
|
||||
- Install LiteIDE
|
||||
- [Download page](http://code.google.com/p/golangide)
|
||||
- [Source code](https://github.com/visualfc/liteide)
|
||||
|
||||
You need to install Go first, then download the version appropriate for your operating system. Decompress the package to directly use it.
|
||||
- Install gocode
|
||||
|
||||
You have to install gocode in order to use intelligent completion
|
||||
|
||||
go get -u github.com/nsf/gocode
|
||||
|
||||
- Compilation environment
|
||||
|
||||
Switch configuration in LiteIDE to suit your operating system.
|
||||
In Windows and using the 64-bit version of Go, you should choose win64 as the configuration environment in the tool bar. Then, choose `opinion`, find `LiteEnv` in the left list and open file `win64.env` in the right list.
|
||||
|
||||
GOROOT=c:\go
|
||||
GOBIN=
|
||||
GOARCH=amd64
|
||||
GOOS=windows
|
||||
CGO_ENABLED=1
|
||||
|
||||
PATH=%GOBIN%;%GOROOT%\bin;%PATH%
|
||||
。。。
|
||||
|
||||
Replace `GOROOT=c:\go` to your Go installation path, save it. If you have MinGW64, add `c:\MinGW64\bin` to your path environment variable for `cgo` support.
|
||||
|
||||
In Linux and using the 64-bit version of Go, you should choose linux64 as the configuration environment in the tool bar. Then, choose `opinion`, find `LiteEnv` in the left list and open the `linux64.env` file in the right list.
|
||||
|
||||
GOROOT=$HOME/go
|
||||
GOBIN=
|
||||
GOARCH=amd64
|
||||
GOOS=linux
|
||||
CGO_ENABLED=1
|
||||
|
||||
PATH=$GOBIN:$GOROOT/bin:$PATH
|
||||
。。。
|
||||
|
||||
Replace `GOROOT=$HOME/go` to your Go installation path, save it.
|
||||
- $GOPATH
|
||||
$GOPATH is the path that contains a list of projects. Open the command tool (or press `Ctrl+` in LiteIDE), then type `go help gopath` for more details.
|
||||
It's very easy to view and change $GOPATH in LiteIDE. Follow `View - Setup GOPATH` to view and change these values.
|
||||
|
||||
## Sublime Text
|
||||
|
||||
Here I'm going to introduce you the Sublime Text 2 (Sublime for short) + GoSublime + gocode + MarGo. Let me explain why.
|
||||
|
||||
- Intelligent completion
|
||||
|
||||

|
||||
|
||||
Figure 1.5 Sublime intelligent completion
|
||||
- Auto-format source files
|
||||
- Project management
|
||||
|
||||

|
||||
|
||||
Figure 1.6 Sublime project management
|
||||
|
||||
- Syntax highlight
|
||||
- Free trial forever with no functional limitations. You may be prompted once in a while to remind you to purchase a licnese, but you can simply ignore it if you wish. Of course, if you do find that it enhances your productivity and you really enjoy using it, please purchase a copy of it and support its continued development!
|
||||
|
||||
First, download the version of [Sublime](http://www.sublimetext.com/) suitable for your operating system.
|
||||
|
||||
1. Press `Ctrl+`, open the command tool and input the following commands.
|
||||
|
||||
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; 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'
|
||||
|
||||
Restart Sublime text when the installation has finished. You should then find a `Package Control` option in the "Preferences" menu.
|
||||
|
||||

|
||||
|
||||
Figure 1.7 Sublime Package Control
|
||||
2. To install GoSublime, SidebarEnhancements and Go Build, press `Ctrl+Shift+p` to open Package Control, then type `pcip` (short for "Package Control: Install Package").
|
||||
|
||||

|
||||
|
||||
Figure 1.8 Sublime Install Packages
|
||||
|
||||
Now type in "GoSublime", press OK to install the package, and repeat the same steps for installing SidebarEnhancements and Go Build. Once again, restart the editor when it completes the installation.
|
||||
3. To verify that the installation is successful, open Sublime, then open the `main.go` file to see if it has the proper syntax highlighting. Type `import` to see if code completion prompts appear. After typing `import "fmt"`, type `fmt.` anywhere after the `import` declaration to see whether or not intelligent code completion for functions was successfully enabled.
|
||||
|
||||
If everything is fine, you're all set.
|
||||
|
||||
If not, check your $PATH again. Open terminal, type `gocode`. If it does not run, your $PATH was not configured correctly.
|
||||
|
||||
## Vim
|
||||
|
||||
Vim is a popular text editor for programmers, which evolved from its slimmer predecessor, Vi. It has functions for intelligent completion, compilation and jumping to errors.
|
||||
|
||||

|
||||
|
||||
Figure 1.8 Vim intelligent completion for Go
|
||||
|
||||
1. Syntax highlighting for Go
|
||||
|
||||
cp -r $GOROOT/misc/vim/* ~/.vim/
|
||||
|
||||
2. Enabling syntax highlighting
|
||||
|
||||
filetype plugin indent on
|
||||
syntax on
|
||||
|
||||
3. Install [gocode](https://github.com/nsf/gocode/)
|
||||
|
||||
go get -u github.com/nsf/gocode
|
||||
|
||||
gocode will be installed in `$GOBIN` as default
|
||||
|
||||
4. Configure [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"
|
||||
|
||||
Explanation of gocode configuration:
|
||||
|
||||
propose-builtins: specifies whether or not to open intelligent completion; false by default.
|
||||
lib-path: gocode only searches for packages in `$GOPATH/pkg/$GOOS_$GOARCH` and `$GOROOT/pkg/$GOOS_$GOARCH`. This setting can be used to add additional paths.
|
||||
|
||||
5. Congratulations! Try `:e main.go` to experience the world of Go!
|
||||
|
||||
## Emacs
|
||||
|
||||
Emacs is the so-called Weapon of God. She is not only an editor, but also a powerful IDE.
|
||||
|
||||

|
||||
|
||||
Figure 1.10 Emacs main panel of Go editor
|
||||
|
||||
1. Syntax highlighting
|
||||
|
||||
cp $GOROOT/misc/emacs/* ~/.emacs.d/
|
||||
|
||||
2. Install [gocode](https://github.com/nsf/gocode/)
|
||||
|
||||
go get -u github.com/nsf/gocode
|
||||
|
||||
gocode will be installed in `$GOBIN` as default
|
||||
3. Configure [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"
|
||||
|
||||
4. Install [Auto Completion](http://www.emacswiki.org/emacs/AutoComplete)
|
||||
Download and uncompress
|
||||
|
||||
~ make install DIR=$HOME/.emacs.d/auto-complete
|
||||
|
||||
Configure ~/.emacs file
|
||||
|
||||
;;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)
|
||||
|
||||
Follow this [link](http://www.emacswiki.org/emacs/AutoComplete) for more details.
|
||||
5. Configure .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. Congratulations, you're done! Speedbar is closed by default -remove the comment symbols in the line `;;(speedbar 1)` to enable this feature, or you can use it through `M-x speedbar`.
|
||||
|
||||
## Eclipse
|
||||
|
||||
Eclipse is also a great development tool. I'll show you how to use it to write Go programs.
|
||||
|
||||

|
||||
|
||||
Figure 1.1 Eclipse main panel for editing Go
|
||||
|
||||
1. Download and install [Eclipse](http://www.eclipse.org/)
|
||||
2. Download [goclipse](https://code.google.com/p/goclipse/)
|
||||
[http://code.google.com/p/goclipse/wiki/InstallationInstructions](http://code.google.com/p/goclipse/wiki/InstallationInstructions)
|
||||
3. Download gocode
|
||||
|
||||
gocode in Github.
|
||||
|
||||
https://github.com/nsf/gocode
|
||||
|
||||
You need to install git in Windows, usually we use [msysgit](https://code.google.com/p/msysgit/)
|
||||
|
||||
Install gocode in the command tool
|
||||
|
||||
go get -u github.com/nsf/gocode
|
||||
|
||||
You can install from source code if you like.
|
||||
4. Download and install [MinGW](http://sourceforge.net/projects/mingw/files/MinGW/)
|
||||
5. Configure plugins.
|
||||
|
||||
Windows->Preferences->Go
|
||||
|
||||
(1).Configure Go compiler
|
||||
|
||||

|
||||
|
||||
Figure 1.12 Go Setting in Eclipse
|
||||
|
||||
(2).Configure gocode(optional), set gocode path to where the gocode.exe is.
|
||||
|
||||

|
||||
|
||||
Figure 1.13 gocode Setting
|
||||
|
||||
(3).Configure gdb(optional), set gdb path to where the gdb.exe is.
|
||||
|
||||

|
||||
|
||||
Figure 1.14 gdb Setting
|
||||
6. Check the installation
|
||||
|
||||
Create a new Go project and hello.go file as following.
|
||||
|
||||

|
||||
|
||||
Figure 1.15 Create new project and file
|
||||
|
||||
Test installation as follows.(you need to type command in console in Eclipse)
|
||||
|
||||

|
||||
|
||||
Figure 1.16 Test Go program in Eclipse
|
||||
|
||||
## IntelliJ IDEA
|
||||
|
||||
People who have worked with Java should be familiar with this IDE. It supports Go syntax highlighting and intelligent code completion, implemented by a plugin.
|
||||
|
||||
1. Download IDEA, there is no difference between the Ultimate and Community editions
|
||||
|
||||

|
||||
|
||||
2. Install the Go plugin. Choose `File - Setting - Plugins`, then click `Browser repo`.
|
||||
|
||||

|
||||
|
||||
3. Search `golang`, double click `download and install` and wait for the download to complete.
|
||||
|
||||

|
||||
|
||||
Click `Apply`, then restart.
|
||||
4. Now you can create a Go project.
|
||||
|
||||

|
||||
|
||||
Input the position of your Go sdk in the next step -basically it's your $GOROOT.
|
||||
|
||||
( ***See a [blog post](http://wuwen.org/tips-about-using-intellij-idea-and-go/) for setup and use IntelliJ IDEA with Go step by step *** )
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Go commands](01.3.md)
|
||||
- Next section: [Summary](01.5.md)
|
||||
|
||||
Reference in New Issue
Block a user