May 7th, 2020

I have been a full-time emacs user for the past 4 years and I have been working in Golang professionally for about 2 years now. Combining those two beauties is quite a task with so many different modules and addons available. This is a comfortable setup I found for myself and I wish to share it. Demo below!

Getting dependencies

One of many great things about Go is its tooling environment. You can write any tools and implement your ideas with ease.

First tool you need is usually Go itself. After that, install some of the go tools dependencies below:

% go get -u golang.org/x/tools/cmd/goimports
% go get -u github.com/rogpeppe/godef
% go get -u github.com/nsf/gocode

Next we need to install some of the emacs dependencies through MELPA. This is gonna be big, prep your .emacs.d for some fun time compiling elisp. Refresh your local repo of packages by running M-x package-refresh-contents. You can install packages by running M-x package-install PACKAGE-NAME

Here is the list:

  • auto-complete - An Intelligent auto-completion extension for Emacs

  • go-mode - Emacs mode for the Go programming language

  • go-complete - Native Go completion for Emacs

  • go-autocomplete - auto-complete-mode backend for go-mode

  • autopair - Automagically pair braces and quotes in emacs like TextMate

Config

You can just put this in your .emacs or whatever init file you’re using. The following config is all you need to get yourself started with automatic code formatting, code completion, parenthesis matching, dot completion (!), and jump to definition.

;; Init the auto complete modules
(ac-config-default)
(global-auto-complete-mode t)
(require 'go-autocomplete)

;; Enable auto-complete
(auto-complete-mode 1)

;; Define keymaps
(define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
(global-set-key (kbd"C-c C-c") 'godef-jump)

;; Set some quick config vals
(setq ac-auto-start 1)
(setq ac-auto-show-menu 0.8)

;; Just to make sure go tools are enabled
(add-to-list 'exec-path "~/go/bin")

;; Automatically format code on save
(setq gofmt-command "goimports")
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook 'auto-complete-for-go)

After that, just reload your emacs and you should have full go environment installed!

Demo

demo.svg
Figure 1. Gopher in emacs