This explains the steps to get a productive Emacs environment for Go programming on OSX, starting from scratch.
I recommend using the emacs from emacsformacosx.com.
It has a GUI installer so I won’t say much more about it.
- At the time of writing, I installed https://storage.googleapis.com/golang/go1.2.2.darwin-amd64-osx10.8.pkg
- After installing the package, you’ll want to define the following environment variables in your
1 2 3
Go-mode is an Emacs major mode for editing Go code. An absolute must for anyone writing Go w/ Emacs.
The following is a brief summary of Dominik Honnef’s instructions
mkdir -p ~/Misc/emacs && cd ~/Misc/emacs
git clone email@example.com:dominikh/go-mode.el.git
- From within Emacs, run
M-x update-file-autoloads, point it at the go-mode.el file in the cloned directory.
- Emacs will prompt you for a result path, and you should enter ~/Misc/emacs/go-mode.el/go-mode-load.el
- Add these two lines to your ~/.emacs (if you don’t already have this file, you should create an empty one)
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
Update Emacs config for
It’s really useful to be able to able to pull up 3rd party or standard library docs from within Emacs using the
Add the following to your
.emacs file so that it gets the PATH environment:
1 2 3 4 5 6 7 8 9 10
(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:
1 2 3
(replace the above path to the absolute path to your
After this step, whenever you save a Go file, it will automatically reformat the file with
Godef is essential: it lets you quickly jump around the code, as you might be used to with a full featured IDE.
From what I can tell, installing go-mode seems to automatically install godef.
To verify that godef is indeed installed:
- Putting the cursor over a method name
- Try doing
M-x godef-jumpto jump into the method, and
M-*to go back.
In order to add godef key bindings, add these to your ~/.emacs:
1 2 3 4 5 6
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
The following is a brief summary of the emacs autocomplete manual
- Download and extract http://cx4a.org/pub/auto-complete/auto-complete-1.3.1.tar.bz2
- Cd into extracted dir and run
emacs -batch -l etc/install.el
- Create a
lispsubdirectory under your
- When prompted for where to install, give it the full path to your
- It will tell you to add the following to your ~/.emacs:
1 2 3 4
To see any effect, we need to install gocode in the next step.
Gocode: Go aware Autocomplete
The following is a brief summary of the gocode README
go get -u -v github.com/nsf/gocode
cp /Users/tleyden/Development/gocode/src/github.com/nsf/gocode/emacs/go-autocomplete.el ~/.emacs.d/
- Add the following to your ~/.emacs
At this point, after you restart emacs, when you start typing something, you should see a popup menu with choices, like this screenshot.
Customize compile command to run
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 and replace your go-mode hook with:
1 2 3 4 5 6 7 8 9 10
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.
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.
Ready for more?
If you’re ready to take it to the next level, check out 5 minutes of go in emacs
(PS: thanks @dlsspy for taking the time to teach me the Emacs wrestling techniques needed to get this far.)
Continue to Part 2
go-imports and go-oracle are covered in Part 2