This explains the steps to get a productive Emacs environment for Go programming on OSX, starting from scratch.
Install Emacs
I recommend using the emacs from emacsformacosx.com.
It has a GUI installer so I won’t say much more about it.
Install lastest version of Go
See Installing Go
After installing, you’ll want to define the following environment variables in your ~/.bash_profile
.
1 2 3 4 |
|
These might be different on your system.
Install additional go tools (godoc, etc)
To get the godoc
tool as well as others, run:
1
|
|
Install Melpa
Melpa is a package manager for Emacs, and is required for go-mode
To configure emacs for melpa:
- Create a new file
~/.emacs.d/init.el
- Add the following contents:
1 2 3 |
|
Restart emacs and run M-x package-list-packages
and you should see it contacting http://melpa.milkbox.net
and you should also see lot of packages listed as being from the melpa archive.
Install go-mode
Run M-x package-install
and when prompted, enter go-mode
and hit enter.
Restart Emacs and open a .go file, you should see the mode as “Go” rather than “Fundamental”.
For a full description of what go-mode can do for you, see Dominik Honnef’s blog, but one really useful thing to be aware of is that you can quickly import packages via C-c C-a
Update Emacs path to find godoc
Run M-x package-install
and enter exec-path-from-shell
I got this warning:
1 2 3 4 5 6 7 8 |
|
Restart emacs.
Update Emacs config for godoc
It’s really useful to be able to able to pull up 3rd party or standard library docs from within Emacs using the godoc
tool.
PATH
Add the following to your ~/.emacs.d/init.el
file so that it gets the PATH environment:
1 2 3 4 5 6 7 8 9 10 |
|
GOPATH
1
|
|
(replace the above path to the absolute path to the directory where you store your Go code)
After doing this step, you should be able to run M-x godoc
and it should be able to autocomplete paths of packages. (of course, you may want to go get
some packages first if you don’t have any)
Automatically call gofmt on save
gofmt
reformats code into the One True Go Style Coding Standard. You’ll want to call it every time you save a file.
Add these to your ~/.emacs.d/init.el
:
1 2 |
|
After this step, whenever you save a Go file, it will automatically reformat the file with gofmt
.
Godef
Godef is essential: it lets you quickly jump around the code, as you might be used to with a full featured IDE.
Install godef itself:
1
|
|
Installing go-mode automatically installs godef bindings.
To verify that godef bindings work:
- Putting the cursor over a method name
- Try doing
M-x godef-jump
to jump into the method, andM-*
to go back.
In order to add godef key bindings, add these to your ~/.emacs.d/init.el
:
1 2 3 4 5 6 7 8 |
|
and remove your previous call to (add-hook 'before-save-hook 'gofmt-before-save)
since it’s now redundant
Now you can jump into code with M-.
and jump back with M-*
Autocomplete
Install melpa auto-complete via M-x package-install
followed by auto-complete
Add the following to your ~/.emacs.d/init.el
file:
1 2 3 |
|
Restart emacs, and if you open a .go file the mode should be Go AC
(AC == AutoComplete)
Before further verifying, we need to install go-autocomplete in the next step.
go-autocomplete
The basic autocomplete installed is not as “go aware” as it should be. The go-autocomplete (aka nsf/gocode) package fixes that.
Install the gocode binary
1
|
|
and then install the melpa package via M-x package-install
followed by go-autocomplete
After installing go-autocomplete
, open your ~/.emacs.d/init.el
file and make sure the following was added:
1 2 3 |
|
If it was not added, you’ll need to add it yourself.
You’ll also need the following (as recommended in gocode issue 325):
1 2 |
|
Now restart emacs, and when you start typing the name of a field in a struct it will popup something that looks like this:
Note that is more “go-aware” than the default auto-complete functionality.
Customize the emacs compile command to run go build
It’s convenient to be able to run M-x compile
to compile and test your Go code from within emacs.
To do that, edit your ~/.emacs.d/init.el
and replace your go-mode hook with:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
After that, restart emacs, and when you type M-x compile
, it should try to execute go build -v && go test -v && go vet
instead of the default behavior. On some projects, you might also want to run go generate
before go build
Power tip: you can jump straight to each compile error by running C-x `
. Each time you do it, it will jump to the next error.
Continue to Part 2
go-imports and go-oracle are covered in Part 2