GitHub - dominikh/go-mode.el: Emacs mode for the Go programming language (original) (raw)
This is go-mode, the Emacs mode for editing Go code.
It is a complete rewrite of the go-mode that shipped with Go 1.0.3 and before, and was part of Go 1.1 until Go 1.3. Beginning with Go 1.4, editor integration will not be part of the Go distribution anymore, making this repository the canonical place for go-mode.
Features
In addition to normal features, such as fontification and indentation, and close integration with familiar Emacs functionality (for example syntax-based navigation like beginning-of-defun), go-mode comes with the following extra features to provide an improved experience:
- Integration with
gofmtby providing a command of the same name, andgofmt-before-save, which can be used in a hook to format Go buffers before saving them.- Setting the
gofmt-commandvariable also allows usinggoimports. - Setting the
gofmt-argsvariable with a list of arguments allows using e.g.gofmt -s.
- Setting the
- Integration with
godocvia the functionsgodocandgodoc-at-point. - Integration with the Playground
go-play-bufferandgo-play-regionto send code to the Playgroundgo-download-playto download a Playground entry into a new buffer
- Managing imports
- A function for jumping to the file's imports (
go-goto-imports-C-c C-f i) - A function for adding imports, including tab completion (
go-import-add, bound toC-c C-a) - It is recommended that you use
goimportsor theorganize-importsfeature ofgoplsto manage adding/removing/organizing imports automatically.
- A function for jumping to the file's imports (
- Integration with godef
godef-describe(C-c C-d) to describe expressionsgodef-jump(C-c C-j) andgodef-jump-other-window(C-x 4 C-c C-j) to jump to declarations- This requires you to install godef via
go install github.com/rogpeppe/godef@latest.
- Basic support for imenu (functions and variables)
- Built-in support for displaying code coverage as calculated by
go test(go-coverage) - Several functions for jumping to and manipulating the individual parts of function signatures. These functions support anonymous functions, but are smart enough to skip them when required (e.g. when jumping to a method receiver or docstring.)
- Jump to the argument list (
go-goto-arguments-C-c C-f a) - Jump to the docstring, create it if it does not exist yet (
go-goto-docstring-C-c C-f d). - Jump to the function keyword (
go-goto-function-C-c C-f f) - Jump to the function name (
go-goto-function-name-C-c C-f n) - Jump to the return values (
go-goto-return-values-C-c C-f r) - Jump to the method receiver, adding a pair of parentheses if no method receiver exists (
go-goto-method-receiver-C-c C-f m).
All of these functions accept a prefix argument (C-u), causing them to skip anonymous functions.
- Jump to the argument list (
Installation
MELPA
The recommended way of installing go-mode is viaELPA, the Emacs package manager, and theMELPA Stable repository, which provides an up-to-date version of go-mode.
If you're not familiar with ELPA yet, consider readingthis guide.
Manual
To install go-mode manually, check out the go-mode.el repository in a directory of your choice, add it to your load path and configure Emacs to automatically load it when opening a .go file:
(add-to-list 'load-path "/place/where/you/put/it/")
(autoload 'go-mode "go-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.go\\'" . go-mode))
Either evaluate the statements with C-x C-e, or restart Emacs.
Other extensions
There are several third party extensions that can enhance the Go experience in Emacs.
Gopls integration
Goplsis the official language server protocol (lsp) implementation provided by the Go team. It is intended to replace the existing third party tools for code formatting (gofmt), automatic imports (goimports), code navigation (godef/guru), type and function descriptions (godoc/godef), error checking, auto completion (gocode), variable and type renaming (rename), and more. Once gopls is stable the older tools will no longer be supported.
Gopls is a supported backend forlsp-mode. It will be used automatically by lsp-mode if gopls is found in your PATH. You can install gopls via: go install golang.org/x/tools/gopls@latest. To enable lsp-mode for go buffers:
(add-hook 'go-mode-hook 'lsp-deferred)
Syntax/error checking
There are two ways of using flymake with Go:
- goflymake, which internally uses
go buildto capture all errors that a regular compilation would also produce - flymake-go for a more lightweight solution that only uses
gofmtand as such is only able to catch syntax errors. Unlike goflymake, however, it does not require an additional executable.
Additionally, there isflycheck, a modern replacement for flymake, which comes with built-in support for Go. In addition to using go build or gofmt, it also has support for go vet,golint and errcheck.
Auto completion
For auto completion, take a look atgocode.
eldoc
https://github.com/syohex/emacs-go-eldoc provides eldoc functionality for go-mode.
Snippets
I maintain a set of YASnippet snippets for go-mode athttps://github.com/dominikh/yasnippet-go
Integration with errcheck
https://github.com/dominikh/go-errcheck.el provides integration witherrcheck.
Stability
go-mode.el has regular, tagged releases and is part of the MELPA Stable repository. These tagged releases are intended to provide a stable experience. APIs added in tagged releases will usually not be removed or changed in future releases.
Changes made on the master branch, which is tracked by the normal MELPA repository, however, are under active development. New APIs are experimental and may be changed or removed before the next release. Furthermore, there is a higher chance for bugs.
If you want a stable experience, use MELPA Stable. If you want cutting edge features, or "beta-test" future releases, use MELPA or the master branch.