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.

1.4. Go development tools - 图1

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
    • Source code

      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

    1. 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 Options, find LiteEnv in the left list and open file win64.env in the right list.

    1. GOROOT=c:\go
    2. GOBIN=
    3. GOARCH=amd64
    4. GOOS=windows
    5. CGO_ENABLED=1
    6. PATH=%GOBIN%;%GOROOT%\bin;%PATH%
    7. 。。。

    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 Options, find LiteEnv in the left list and open the linux64.env file in the right list.

    1. GOROOT=$HOME/go
    2. GOBIN=
    3. GOARCH=amd64
    4. GOOS=linux
    5. CGO_ENABLED=1
    6. PATH=$GOBIN:$GOROOT/bin:$PATH
    7. 。。。

    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 3 (Sublime for short) + GoSublime + gocode. Let me explain why.

  • Intelligent completion

    1.4. Go development tools - 图2

    Figure 1.5 Sublime intelligent completion

  • Auto-format source files
  • Project management

    1.4. Go development tools - 图3

    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 license, 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 suitable for your operating system.

  1. Press Ctrl+` , open the command tool and input the following commands.

    Applicable to Sublime Text 3:

  1. 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())

Applicable to Sublime Text 2:

  1. 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')
  1. Restart Sublime Text when the installation has finished. You should then find a `Package Control` option in the "Preferences" menu.
  2. ![](images/1.4.sublime3.png?raw=true)
  3. Figure 1.7 Sublime Package Control
  1. To install GoSublime, SidebarEnhancements and Go Build, press Ctrl+Shift+p to open Package Control, then type pcip (short for “Package Control: Install Package”).

    1.4. Go development tools - 图4

    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.

  2. 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 a 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.

vim-go is vim above an open-source go language using the most extensive development environment plug-ins

The plugin address:github.com/fatih/vim-go

Vim plugin management are the mainstream Pathogen and Vundle ,But the aspects thereof are different. Pathogen is to solve each plug-in after the installation of files scattered to multiple directories and poor management of the existence. Vundle is to solve the automatic search and download plug-ins exist. These two plug-ins can be used simultaneously.

1.Install Vundle

  1. mkdir ~/.vim/bundle
  2. git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Edit .vimrc,Vundle the relevant configuration will be placed in the beginning(Refer to the Vundle documentation for details)

  1. set nocompatible " be iMproved, required
  2. filetype off " required
  3. " set the runtime path to include Vundle and initialize
  4. set rtp+=~/.vim/bundle/Vundle.vim
  5. call vundle#begin()
  6. " let Vundle manage Vundle, required
  7. Plugin 'gmarik/Vundle.vim'
  8. " All of your Plugins must be added before the following line
  9. call vundle#end() " required
  10. filetype plugin indent on " required

2.Install Vim-go

Edit ~/.vimrc,Add a line between vundle #begin and vundle #end:

  1. Plugin 'fatih/vim-go'

Executed within Vim: PluginInstall

3.Install YCM(Your Complete Me) to AutoComplete Add a line to ~ / .vimrc:

  1. Plugin 'Valloric/YouCompleteMe'

Executed within Vim: PluginInstall

1.4. Go development tools - 图5

Figure 1.8 Vim intelligent completion for Go

  1. Syntax highlighting for Go

    1. cp -r $GOROOT/misc/vim/* ~/.vim/
  2. Enabling syntax highlighting

    1. filetype plugin indent on
    2. syntax on
  3. Install gocode

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

    gocode will be installed in $GOBIN as default

  4. Configure gocode

    1. ~ cd $GOPATH/src/github.com/nsf/gocode/vim
    2. ~ ./update.sh
    3. ~ gocode set propose-builtins true
    4. propose-builtins true
    5. ~ gocode set lib-path "/home/border/gocode/pkg/linux_amd64"
    6. lib-path "/home/border/gocode/pkg/linux_amd64"
    7. ~ gocode set
    8. propose-builtins true
    9. 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.

1.4. Go development tools - 图6

Figure 1.10 Emacs main panel of Go editor

  1. Syntax highlighting

    1. cp $GOROOT/misc/emacs/* ~/.emacs.d/
  2. Install gocode

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

    gocode will be installed in $GOBIN as default

  3. Configure gocode

    1. ~ cd $GOPATH/src/github.com/nsf/gocode/vim
    2. ~ ./update.bash
    3. ~ gocode set propose-builtins true
    4. propose-builtins true
    5. ~ gocode set lib-path "/home/border/gocode/pkg/linux_amd64"
    6. lib-path "/home/border/gocode/pkg/linux_amd64"
    7. ~ gocode set
    8. propose-builtins true
    9. lib-path "/home/border/gocode/pkg/linux_amd64"
  4. Install Auto Completion Download and uncompress

    1. ~ make install DIR=$HOME/.emacs.d/auto-complete

    Configure ~/.emacs file

    1. ;;auto-complete
    2. (require 'auto-complete-config)
    3. (add-to-list 'ac-dictionary-directories "~/.emacs.d/auto-complete/ac-dict")
    4. (ac-config-default)
    5. (local-set-key (kbd "M-/") 'semantic-complete-analyze-inline)
    6. (local-set-key "." 'semantic-complete-self-insert)
    7. (local-set-key ">" 'semantic-complete-self-insert)

    Follow this link for more details.

  5. Configure .emacs

    1. ;; golang mode
    2. (require 'go-mode-load)
    3. (require 'go-autocomplete)
    4. ;; speedbar
    5. ;; (speedbar 1)
    6. (speedbar-add-supported-extension ".go")
    7. (add-hook
    8. 'go-mode-hook
    9. '(lambda ()
    10. ;; gocode
    11. (auto-complete-mode 1)
    12. (setq ac-sources '(ac-source-go))
    13. ;; Imenu & Speedbar
    14. (setq imenu-generic-expression
    15. '(("type" "^type *\\([^ \t\n\r\f]*\\)" 1)
    16. ("func" "^func *\\(.*\\) {" 1)))
    17. (imenu-add-to-menubar "Index")
    18. ;; Outline mode
    19. (make-local-variable 'outline-regexp)
    20. (setq outline-regexp "//\\.\\|//[^\r\n\f][^\r\n\f]\\|pack\\|func\\|impo\\|cons\\|var.\\|type\\|\t\t*....")
    21. (outline-minor-mode 1)
    22. (local-set-key "\M-a" 'outline-previous-visible-heading)
    23. (local-set-key "\M-e" 'outline-next-visible-heading)
    24. ;; Menu bar
    25. (require 'easymenu)
    26. (defconst go-hooked-menu
    27. '("Go tools"
    28. ["Go run buffer" go t]
    29. ["Go reformat buffer" go-fmt-buffer t]
    30. ["Go check buffer" go-fix-buffer t]))
    31. (easy-menu-define
    32. go-added-menu
    33. (current-local-map)
    34. "Go tools"
    35. go-hooked-menu)
    36. ;; Other
    37. (setq show-trailing-whitespace t)
    38. ))
    39. ;; helper function
    40. (defun go ()
    41. "run current buffer"
    42. (interactive)
    43. (compile (concat "go run " (buffer-file-name))))
    44. ;; helper function
    45. (defun go-fmt-buffer ()
    46. "run gofmt on current buffer"
    47. (interactive)
    48. (if buffer-read-only
    49. (progn
    50. (ding)
    51. (message "Buffer is read only"))
    52. (let ((p (line-number-at-pos))
    53. (filename (buffer-file-name))
    54. (old-max-mini-window-height max-mini-window-height))
    55. (show-all)
    56. (if (get-buffer "*Go Reformat Errors*")
    57. (progn
    58. (delete-windows-on "*Go Reformat Errors*")
    59. (kill-buffer "*Go Reformat Errors*")))
    60. (setq max-mini-window-height 1)
    61. (if (= 0 (shell-command-on-region (point-min) (point-max) "gofmt" "*Go Reformat Output*" nil "*Go Reformat Errors*" t))
    62. (progn
    63. (erase-buffer)
    64. (insert-buffer-substring "*Go Reformat Output*")
    65. (goto-char (point-min))
    66. (forward-line (1- p)))
    67. (with-current-buffer "*Go Reformat Errors*"
    68. (progn
    69. (goto-char (point-min))
    70. (while (re-search-forward "<standard input>" nil t)
    71. (replace-match filename))
    72. (goto-char (point-min))
    73. (compilation-mode))))
    74. (setq max-mini-window-height old-max-mini-window-height)
    75. (delete-windows-on "*Go Reformat Output*")
    76. (kill-buffer "*Go Reformat Output*"))))
    77. ;; helper function
    78. (defun go-fix-buffer ()
    79. "run gofix on current buffer"
    80. (interactive)
    81. (show-all)
    82. (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.

1.4. Go development tools - 图7

Figure 1.1 Eclipse main panel for editing Go

  1. Download and install Eclipse
  2. Download goclipse http://code.google.com/p/goclipse/wiki/InstallationInstructions
  3. Download gocode

    gocode in Github.

    1. https://github.com/nsf/gocode

    You need to install git in Windows, usually we use msysgit

    Install gocode in the command tool

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

    You can install from source code if you like.

  4. Download and install MinGW
  5. Configure plugins.

    Windows->Preferences->Go

    (1).Configure Go compiler

    1.4. Go development tools - 图8

    Figure 1.12 Go Setting in Eclipse

    (2).Configure gocode(optional), set gocode path to where the gocode.exe is.

    1.4. Go development tools - 图9

    Figure 1.13 gocode Setting

    (3).Configure gdb(optional), set gdb path to where the gdb.exe is.

    1.4. Go development tools - 图10

    Figure 1.14 gdb Setting

  6. Check the installation

    Create a new Go project and hello.go file as following.

    1.4. Go development tools - 图11

    Figure 1.15 Create a new project and file

    Test installation as follows.(you need to type command in console in Eclipse)

    1.4. Go development tools - 图12

    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

    1.4. Go development tools - 图13

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

    1.4. Go development tools - 图14

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

    1.4. Go development tools - 图15

    Click Apply, then restart.

  4. Now you can create a Go project.

    1.4. Go development tools - 图16

    Input the position of your Go sdk in the next step -basically it’s your $GOROOT.

( See a blog post for setup and use IntelliJ IDEA with Go step by step )

Visual Studio VSCode

This is an awesome text editor released as open source cross platform by Microsoft which takes the development experience to a whole new level, https://code.visualstudio.com/. It has everything a modern text editor is expected to have and despite being based on the same backend that atom.io is based, it is very fast.

It works with Windows, Mac, Linux. It has go package built, it provides code linting.

Atom

Atom is an awesome text editor released as open source cross platform, built on Electron , and based on everything we love about our favorite editors. We designed it to be deeply customizable, but still approachable using the default configuration.

Download: https://atom.io/

GoLand

GoLand is the codename for a new commercial IDE by JetBrains aimed at providing an ergonomic environment for Go development.

Download: https://www.jetbrains.com/go/