Golang on Doom Emacs

Nov 21, 2021
Walter Manger
2 minute read
Photo by Patrice Schoefolt from Pexels

Photo by Patrice Schoefolt from Pexels

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:

  1. The ability to navigate through code
  2. The ability to see documentation
  3. Syntax checking
  4. 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/[email protected]

# Autocompletion
go install github.com/stamblerre/[email protected]

# Documentation
go install golang.org/x/tools/cmd/[email protected]

# Add/Removed Necessary Imports
go install golang.org/x/tools/cmd/[email protected]

# Type-Safe Renaming of Go identifiers
go install golang.org/x/tools/cmd/[email protected]

# Asks questions about your Gocode
go install golang.org/x/tools/cmd/[email protected]

# Generate tests based off of the func you're on
go install github.com/cweill/gotests/[email protected]

# Add `json` or `bson` to structs easily
go install github.com/fatih/[email protected]

With this configuration I now have:

  1. The ability to navigate through code by using g d on a particular identifier
  2. The ability to see documentation by simply hovering over an identifier
  3. Syntax checking on every save
  4. The ability to generate and test code by:
    • SPC m t g while in the body of a func to generate test code
    • SPC m t s while in the body of a test func for running that one test
    • SPC 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.