[deleted] · 2022-01-25 15:15
[removed]
d_metal_ · 2022-01-25 16:10
You can start setting up lsp with go:
https://emacs-lsp.github.io/lsp-mode/page/lsp-gopls/
_pkal · 2022-01-25 16:32
It seems that most Go utilities have been submerged into [gopls](https://github.com/golang/tools/blob/master/gopls/README.md), the LSP server. So a lot of older websites that suggest how to configure [go-mode](https://github.com/dominikh/go-mode.el) might be outdated, because of the cultural shift away from the _unix-ish little, collaborative utilities_ approach. This means you will need a LSP client -- the Go project has [some documentation](https://cs.opensource.google/go/x/tools/+/refs/tags/gopls/v0.7.5:gopls/doc/emacs.md) on that. I'd strongly recommend Eglot, if you are uncertain what to use(0). [This page](https://pkg.go.dev/golang.org/x/tools/gopls#readme-installation) explains how to install gopls with your Go toolchain.
I think that the one tool that might still be useful outside of gopls is [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports). It can be used as a gofmt replacement, that also automatically manages and removes imports. Gopls can integrate [staticcheck](https://staticcheck.io/) and [gofumpt](https://github.com/mvdan/gofumpt), but my understanding is that they have to be installed manually. See the [settings](https://cs.opensource.google/go/x/tools/+/refs/tags/gopls/v0.7.5:gopls/doc/settings.md) section for more on that(1).
On `go-mode` itself, this is the configuration that I use (it is written using setup.el, and I'm too lazy to translate it right now because I think it is clear enough, but if you don't get something, I can explain what is going on):
;;;;; Go
(setup (:if-package go-mode)
(:option (append exec-path) (expand-file-name "~/.local/go/bin")
gofmt-command "goimports")
(:local-hook before-save-hook gofmt)
(:local-set compile-command "go build && go vet")
(:hook yas-minor-mode subword-mode)
(setenv "GOPATH" (expand-file-name "~/.local/go"))
(:bind "C-c C-d" godoc-at-point))
As for Eglot and co. this might give you a few pointers as to what can be configured
;;;;; LSP client
(setup (:if-package eglot)
(:option eglot-autoshutdown t
eldoc-echo-area-use-multiline-p nil
eldoc-idle-delay 0.1)
(:bind "C-c r" eglot-rename)
(:unbind [remap display-local-help]))
;;;;; Syntax checker
(setup flymake
(:bind "M-n" flymake-goto-next-error
"M-p" flymake-goto-prev-error)
;; I cannot remember the names of the diagnostics commands
(defalias 'list-issues #'flymake-show-buffer-diagnostics)
(defalias 'list-all-issues #'flymake-show-project-diagnostics)
(:option help-at-pt-display-when-idle t)
(:hook-into prog-mode))
;;;;; Xref (Cross-Referencing)
(setc xref-show-definitions-function 'xref-show-definitions-buffer-at-bottom
xref-search-program 'ripgrep)
Other packages worth looking into might be yasnippet, if you want to avoid tying `if err != nil { ... }` out and company/corfu for popup autocompletion.
Sadly go-mode has been a bit stagnant in terms of developemnt over the last few years, but it seems that there was recently some activity that might mean things will improve (1.6.0 finally [bumped](https://github.com/dominikh/go-mode.el/commit/3273fcece5d9ab7edd4f15b2d6bce61f4e5a0666#) after almost [5 years](https://github.com/dominikh/go-mode.el/commit/35f6826e435c3004dabf134d0f2ae2f31ea7b6a2)).
---
(0) I always get the impression that the alternative, lsp-mode is less interested in integrating into Emacs, it's customs and best practices, and more into making Emacs a VSCode-clone
(1) Note that the Emacs configuration page has a typo when giving an example for how to use `.dir-locals.el`. You need to write `gopls` as a keyword (`:gopls`) so that Eglot uses that confiuration. E.g. in a recent project I have been working on, it looks like this:
((nil . ((eglot-workspace-configuration . ((:gopls (staticcheck . t)))))))