Using Go on GNU Emacs 29 or later

The 29th version of GNU Emacs is probably going to be released sometime this year (2023). This version introduces awesome new features such as:

  • eglot, a Language Server Protocol client
  • use-package for simplified package configuration
  • treesit source code parsing with tree-sitter

These features makes it possible to have a complete development environment for Go with built-in GNU Emacs packages. In order to have syntax highlight and other features1, use the following on your configuration file:

(use-package treesit
  :preface
  (dolist (mapping '((go-mode . go-ts-mode)))
    (add-to-list 'major-mode-remap-alist mapping))
  :init
  (add-to-list 'auto-mode-alist '("\\.go\\'" . go-ts-mode))
  (add-to-list 'auto-mode-alist '("/go\\.mod\\'" . go-mod-ts-mode)))

For the LSP client, you can write the following to automatically connect to gopls when you open a Go file:

(use-package eglot
  :hook ((go-ts-mode . eglot-ensure))

This should be enough to give you LSP support, syntax highlighting and error diagnostics. All of this already bundled on GNU Emacs!