#Go development tools In this section, I'm going to show you some IDEs that have abilities of intelligent completion and auto-format. There are all cross-platform, so the steps I show you should not be very different if you are not using same operating system.
##LiteIDE LiteIDE is an open source, lightweight IDE for developing Go project only, developed by visualfc.
Figure 1.4 Main panel of LiteIDE
LiteIDE features.
- Cross-platform
- Windows
- Linux
- Mac OS
- Cross-compile
- Manage multiple compile environment
- Supports cross-compile of Go
- Project management standard
- Documentation view based on $GOPATH
- Compile 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 by
F1
- Function declaration jump by
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
You need to install Go first, then download the version of your operating system. Decompress the package to direct use.
-
Install gocode
You have to install gocode in order to use intelligent completion
go get -u github.com/nsf/gocode
-
Compile environment
Switch configuration in LiteIDE that fits your operating system. In Windows and 64-bit version of Go, you should choose win64 in environment configuration in tool bar. Then you choose
opinion
, findLiteEnv
in the left list, open filewin64.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, addc:\MinGW64\bin
to your path environment variable forcgo
support.In Linux and 64-bit version of Go, you should choose linux64 in environment configuration in tool bar. Then you choose
opinion
, findLiteEnv
in the left list, open filelinux64.env
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 project list, open command tool (or press
Ctrl+
in LiteIDE) then typego help gopath
for more details. It's very easy to view and change $GOPATH in the LiteIDE. FollowView - 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 trail forever, no functions limit. It will pop-up unregistered prompt sometimes, but you can just ignore it.
First, download the version of Sublime that fits your operating system.
-
Press
Ctrl+
` open command tool, input 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 when installation finished. Then you can find
Package Control
item in the Preferences menu.Figure 1.7 Sublime Package Control
-
To install GoSublime, SidebarEnhancements and Go Build, press
Ctrl+Shift+p
to open Package Control, then typepcip
(short for "Package Control: Install Package").Figure 1.8 Sublime Install Packages
Now you type GoSublime, press OK to install, same steps for installing SidebarEnhancements and Go Build. Restart when it finished installation.
-
To verify if installation is successful, open Sublime, then open file
main.go
to see if it has syntax highlight, typeimport
to see if it has prompts, after typedimport "fmt"
, typefmt.
to see if it has intelligent completion for functions.If everything is fine, you're all set.
If not, check your $PATH again. Open terminal, type
gocode
, if it cannot run, your $PATH must not configure correctly.
##Vim Vim a popular text editor for programmers, which is developing from vi. It has functions for intelligent completion, compile and jump to errors.
Figure 1.8 Vim intelligent completion for Go
-
Syntax highlight for Go
cp -r $GOROOT/misc/vim/* ~/.vim/
-
Set syntax highlight on
filetype plugin indent on syntax on
-
Install gocode
go get -u github.com/nsf/gocode
gocode will be installed in
$GOBIN
as default -
Configure 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 set.
propose-builtins: whether open intelligent completion or not, false as default.
lib-path: gocode only search packages in
$GOPATH/pkg/$GOOS_$GOARCH
and$GOROOT/pkg/$GOOS_$GOARCH
, this setting can add additional path. -
Congratulations! Try
:e main.go
to experience the world of Go!
##Emacs Emacs is so-called Weapon of God. She is not only an editor, also a powerful IDE.
Figure 1.10 Emacs main panel of Go editor
-
Syntax highlight
cp $GOROOT/misc/emacs/* ~/.emacs.d/
-
Install gocode
go get -u github.com/nsf/gocode
gocode will be installed in
$GOBIN
as default -
Configure 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"
-
Install Auto Completion 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 for more details.
-
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"))
-
Congratulations! speedbar is closed as default, cut comment symbols in line
;;(speedbar 1)
to have this feature, or you can have it throughM-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 of Go editor
-
Download and install Eclipse
-
Download goclipse http://code.google.com/p/goclipse/wiki/InstallationInstructions
-
Download gocode
gocode in Github.
https://github.com/nsf/gocode
You need to install git in Windows, usually we use msysgit
Install gocode in the command tool
go get -u github.com/nsf/gocode
You can install from source code if you like.
-
Download and install MinGW
-
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
-
Check 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 are working with Java should be familiar with this IDE, it supports Go syntax highlight, intelligent completion and reconstructed by a plugin.
-
Download IDEA, there is no different from Ultimate and Community Edition
-
Install Go plugin. Choose
File - Setting - Plugins
, then clickBrowser repo
. -
Search
golang
, double clickdownload and install
, wait for downloading.Click
Apply
, then restart. -
Now you can create Go project.
Input position of go sdk in the next step, basically it's your $GOROOT.
##Links
- Directory
- Previous section: Go commands
- Next section: Summary