Go is Google's formerly in-house systems programming language. At least one of the Google maintainers is an Emacs user, leading to first-class support for Emacs as a Go editor.
Go works well with autocomplete
, and has a lot of nice tools:
- godoc
- godef
- gofmt
- gocode
Configuring Emacs for Go
To turn Emacs into a first-class Go editor, you'll need to do a few things.
Install Go
You can install Go with package managers on most Linux distributions,
but it's also pretty easy to do it yourself and closely track Go
releases. Download the most recent version from the downloads
page, and then untar the downloaded tarball
into your /usr/local
tree:
tar -C /usr/local -xzf go1.11.4.linux-amd64.tar.gz
Update Path
Update your .bashrc
to include Go binaries in the PATH
variable, like this:
# Include Go binaries in PATH
export PATH=${PATH}:/usr/local/go/bin # or wherever you put it
Make the Go home
Go (if you're not using modules) requires you to keep your source code in the "Go home", typically ~/go
. Go ahead (no pun intended) and make this directory:
mkdir ~/go
Install tools
Install the tools you need from the Golang package repository. For my setup, I install the godoc
, gofmt
, godef
, and gocode
tools:
go get golang.org/x/tools/cmd/...
go get github.com/rogpeppe/godef
go get -u github.com/nsf/gocode
Configure Emacs
You need to define four variables for go-mode
and company-go
:
godoc-and-godef-command
should be set to "/usr/local/go/bin/godoc"godef-command
should be set to "~/go/bin/godef"gofmt-command
should be set to "/usr/local/go/bin/gofmt"company-go-gocode-command
should be set to "~/go/bin/gocode"
My full configuration looks like this:
;; Go
(require 'company-go)
(with-eval-after-load 'go-mode
(setenv "PATH" (concat "/usr/local/go/bin" ":" "~/go/bin" ":" (getenv "PATH")))
(defconst godoc-and-godef-command "/usr/local/go/bin/godoc")
(defconst godef-command "~/go/bin/godef")
(defconst gofmt-command "/usr/local/go/bin/gofmt")
(defconst company-go-gocode-command "~/go/bin/gocode")
(local-set-key (kbd "M-g M-d") 'godoc-at-point))
(mapc (lambda (mode)
(add-hook 'go-mode-hook mode))
(list 'rainbow-mode
'hs-minor-mode
'hideshowvis-enable
'diff-hl-mode
'projectile-mode
'flycheck-mode))
(add-hook 'go-mode-hook
(lambda ()
(set (make-local-variable 'company-backends) '(company-go))
(company-mode)))
References
- Seven Story Rabbit
Hole
has a good guide to this, but some of the instructions are
outdated. I also use
company-go
instead ofgo-autocomplete
. - As usual, the Arch wiki has good stuff on Go, but not as much on Emacs.
- The Go website has good tips on getting Go installed.