Golang on Doom Emacs
It’s been a while since I’ve contributed to any Golang , but I got the itch this weekend to setup my Doom Emacs to do so.
Most of the time I don’t want a full-blown IDE experience as having all the bells and whistles of that type of environment can be distracting. Instead, I’d like to turn on features as I need them.
Here are the things I’d like to have in a Golang editor:
- The ability to navigate through code
- The ability to see documentation
- Syntax checking
- The ability to generate and test code
The first thing I need is Golang.
brew install go
Using brew will ensure that go
is in my path, but I’ll need actual Go packages in my path too.
For this, I’ll add GOPATH
to my PATH
.
In ~/.zshrc
…
export GOPATH="$(go env GOPATH)"
export PATH="${PATH}:${GOPATH}/bin"
You may have to tell Emacs about the changes you made to your path.
~/.emacs.d/bin/doom env # generates a new env in ~/.emacs.d/.local/env
Now, that go
and any tools installed via go install
are available in my path, I’ll configure Emacs for Go.
In ~/.doom.d/init.el
(doom!
:tools
lsp
:lang
(go +lsp))
Finally, I can install all the tools that Doom’s default Go configuration needs.
# REPL!
go install github.com/x-motemen/gore/cmd/gore@latest
# Autocompletion
go install github.com/stamblerre/gocode@latest
# Documentation
go install golang.org/x/tools/cmd/godoc@latest
# Add/Removed Necessary Imports
go install golang.org/x/tools/cmd/goimports@latest
# Type-Safe Renaming of Go identifiers
go install golang.org/x/tools/cmd/gorename@latest
# Asks questions about your Gocode
go install golang.org/x/tools/cmd/guru@latest
# Generate tests based off of the func you're on
go install github.com/cweill/gotests/gotests@latest
# Add `json` or `bson` to structs easily
go install github.com/fatih/gomodifytags@latest
With this configuration I now have:
- The ability to navigate through code by using
g d
on a particular identifier - The ability to see documentation by simply hovering over an identifier
- Syntax checking on every save
- The ability to generate and test code by:
SPC m t g
while in the body of a func to generate test codeSPC m t s
while in the body of a test func for running that one testSPC m t a
to run all tests
These tools and packages give me way more than what is listed here, but these are the nice-to-haves to get started.