Golang Development Environment

Installation

  • Download go
  • Go Complier:
    • offcial: gc
    • GNU GC: gccgo
  • Go supports instruction sets
    1. amd64 (a.k.a. x86-64); 6g,6l(Plan9 gc),6c,6a // support x84-64, naming as amd64
      is to amd’s contribution of inventing 64bit instruction set
    2. 386 (a.k.a. x86 or x86-32); 8g,8l,8c(Plan9 gc),8a
    3. arm (a.k.a. ARM); 5g,5l,5c(Plan9 gc),5a. Now supports 64-bit ARM architecture on FreeBSD 12.0 or later (the freebsd/arm64 port).
    4. Experimental support for 64-bit RISC-V on Linux (GOOS=linux, GOARCH=riscv64)
  • Set Env Variables
    • GOBIN: C:\Go\bin (optional, if no GOBIN, GOPATH will be used)
    • GOARCH: x86-32bit: 386, x86-64bit: amd64, ARM: amr (android)
    • GOOS: windows
    • GOROOT: C:\Go
    • GOPATH: C:\GOPATH
      • To specify directories outside of $GOROOT that contain the source for Go projects and their binaries.
    • Add to PATH: %GOBIN%
  • Multi-Version GO Env
    • GVM
      • An interface to manage Go versions, like NVM(Nodejs Version Manager), RVM (Ruby Version Manager).
  • Reference in astaxie/build-web-application-with-golang

Tools

IDE

GOPROXY

  • Set to Goproxy China
  • default value: GOPROXY=https://proxy.golang.org,direct
1
2
$ go env -w GO111MODULE=on
$ go env -w GOPROXY=https://goproxy.cn,direct

done.

macOS or Linux

Open your terminal and execute

1
2
$ export GO111MODULE=on
$ export GOPROXY=https://goproxy.cn

or

1
2
3
$ echo "export GO111MODULE=on" >> ~/.profile
$ echo "export GOPROXY=https://goproxy.cn" >> ~/.profile
$ source ~/.profile

done.

Windows

Open your PowerShell and execute

1
2
C:\> $env:GO111MODULE = "on"
C:\> $env:GOPROXY = "https://goproxy.cn"

gopls

  • It is widely used in vscode, vim/neovim for golang IDE.
    • gopls (pronounced: “go please”) is the official language server for the Go language.
    • It is currently in alpha, so it is not stable. Aiming stable in the first half of 2020.
  • Installation
    • By vscode or vim-go
    • Manually
      • Set GOPROXY in above steps.
      • go get golang.org/x/tools/gopls@latest, note, don’t use -u option to keep it compatible.
  • Problems of gopls
    • slow in searching implemenations of interface, callers and callees.
    • CPU and memory consuming (2G ~ 4G).

Vscode/insiders

  • Install Go plugin with tools (gopls, godef, gorename…)
    • command palette: “Go:install/update tools”
  • Setup Configuration
1
2
3
4
5
6
7
8
9
"go.autocompleteUnimportedPackages": true,
"go.inferGopath": true,
"go.gopath": "C:\\GOPATH",
"go.goroot": "C:\\Go",
"go.testFlags": ["-v"],
"go.testOnSave": true,
"go.toolsGopath": "C:\\Go",
"go.formatTool": "goimports",
"go.useLanguageServer": true

Vim

  • vim-go
    • PluginInstall
      • Plug: Plug ‘fatih/vim-go’, { ‘do’: ‘:GoUpdateBinaries’ }
      • Vundle: Plugin ‘fatih/vim-go’
    • GoInstallBinaries/GoUpdateBinaries
    • Install ctags: e.g. In Ubuntu, sudo apt-get install ctags and in /usr/bin/ctags
    • Config the ctags path for tagbar: let g:tagbar_ctags_bin = ‘/usr/bin/ctags’ (Ubuntu)
  • Tutorial
    • GoGuruScope
  • Configurations
1
2
3
4
5
6
7
let g:go_auto_type_info = 1
let g:go_auto_sameids = 1
"let g:go_debug = ['lsp']
"let g:go_gopls_enabled = 0
"let g:go_def_mode = 'guru'
"let g:go_referrers_mode = 'guru'
"let g:go_info_mode = 'guru' "default is gopls
1
2
3
" Golang
au BufNewFile,BufRead *.go setf go
autocmd FileType go setlocal omnifunc=go#complete#Complete
  • If no config, use the gvim-ex which support it by default (unknown why)
  • autocompletion is dependent on the gocode, when golang upgrade, please upgrade the gocode by go get -u github.com/nsf/gocode
  • YouCompleteMe (Code-completion for Vim, but no official support on Windows)

  • Supports in cygwin vim, using imported env variable from Windows. Currently found godef has limitation in cygwin to browse the code definition in $GOPATH/src(config problem?)

  • godef issue in go v1.4

    • Need to update the godef and build it by self

Gogland

  • JetBrains GO IDE
    • Disable parameter name hint (Editor -> General -> Appearance)

sublime

  • Install package control
    • “ctrl + `“ or Menu: View -> Show console: input
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())
  • Restart sublime
  • Install GoSublime
    • ctrl+shift+p -> input install -> input GoSublime
    • preferences —> package setting —> gosublime —> setting default, update env
    • Restart sublime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"env": {
"path":"C:\\Go\\bin",
"GOROOT":"C:\\Go",
"GOPATH":"C:\\GOPATH",
"GOARCH":"amd64",
"GOOS":"windows",
"PATH":"%GOBIN%;%PATH%"
},
"comp_lint_enabled": true,
"comp_lint_commands": [
{"cmd": ["go", "install"]}
],
"on_save": [
{"cmd":"gs_comp_lint"}
]
}
  • ctrl+p -> gosublime: to find related gosublime functions and key binding

  • keybinds: ctrl+.,ctrl+g: definition

  • Build

    • ctrl+b (cli console in sublime), then run the go commands
  • gosublime drawback: read code with goto definition but no easy way to go back.

  • Ref:

Atom

  • Lots of golang package support
    • go-plus (but currently failed to install in my workspace)

Notepad++

  • Gonpp

intellij

Gogland

JetBrains, commercial IDE, extends the IntelliJ platform

  • Not formal release yet. Can download an early build for trial

Eclipse

  • Goclipse

Wide

Zeus IDE

  • TBD

LiteIDE

LiteIDE is a simple, open source, cross-platform Go IDE. Based on Qt5/Qt4

  • easy installation and usage
  • based on Qt, gui is not fashion style