emacs-prelude/docs/modules/go.md
Liam ab85208b7b
Move go doc to new location (#1366)
Includes a couple other small fixes.
2022-04-18 13:05:15 +03:00

3.3 KiB

Prelude Go

This module builds on top of the shared Programming module, as well as the prelude-lsp module.

The following keybindings are set by default, which are not present in each mode's default bindings:

  • C-c a (go-test-current-project)
  • C-c m (go-test-current-file)
  • C-c . (go-test-current-test)
  • C-c b (go-run)
  • C-h f (godoc-at-point)

Run C-h m for all the key bindings and other documentation.

There are two ways to manage projects in Go: $GOPATH and with Go modules. Modules are the newer, recommended method. Read Using Go Modules to learn about this, if you are unfamiliar with the subject. Many of the tools used by Prelude Go may provide functions that are broken with modules. There is usually another function that will work properly; when in doubt, use a function provided by lsp-mode which is documented below.

Generics were added to Go in 1.18. gopls, the backend for lsp-mode setup herein, supports generics as long as gopls itself was built with 1.18+. Other minor modes may not support generics yet.

Go Mode

prelude-go builds on several useful Go tools, and establishes sensible defaults. The major mode is go-mode. Documentation is available at github.com/dominikh/go-mode.el

Go Projectile

Projectile integration is provided by go-projectile.

This provides:

  • Projectile integration
  • Switching GOPATH if desired per project (customizable via customize)
  • Ability to download all commonly used go tools via M-x go-projectile-install-tools and update them via M-x go-projectile-update-tools
  • Very basic refactoring via go-projectile-rewrite (uses gofmt -r)
  • Support for go get and go get -u via go-projectile-get and go-projectile-update.

See its documentation for details.

LSP Mode and LSP UI

LSP (Language Server Protocol) is a protocol that allows editors to use an external "language server" to provide features like autocompletion, documentation, and code navigation rather than implementing these features separately in each editor. Emacs supports LSP via lsp-mode. The language server used is gopls.

To install gopls, change to a directory outside of $GOPATH or any module (e.g., /tmp) and execute:

go install golang.org/x/tools/gopls@latest

Ensure that gopls is in your $PATH.

Excellent documentation for lsp-mode and lsp-ui are provided at emacs-lsp.github.io/lsp-mode/

If a feature, such as documentation, refactoring, indenting, etc. is provided by lsp, you should use it instead of calling to another tool. gopls is the officially maintained tool that supercedes functionality in other tools, like gocode, and works properly with modules and generics.

Company support is automatically added that works with lsp.

GoTest

gotest is also provided while editing Go files in order to run tests more easily. The bindings provided by prelude-go are listed at the top because gotest does not set any.