Improve the "Go Programming" section and rely on format-all

This commit is contained in:
Gerard Vermeulen 2025-02-06 12:57:55 +01:00
parent 3f061da8cd
commit 072b0056b3

View File

@ -4437,25 +4437,72 @@ Listing [[lst:bug-reports]] sets up [[info:emacs#Bug Reference][Bug Reference Mo
[[https://jorge.olano.dev/blog/getting-started-with-go-and-emacs/][Getting started with Go and Emacs]] looks interesting but this setup uses ~eglot~
~go-ts-mode~ (instead of ~go-mode~) and my hardware is too old to use [[https://brew.sh/][HomeBrew]].
This setup uses the following links to install [[https://go.dev/][Go]] and [[https://pkg.go.dev/golang.org/x/tools/gopls][gopls]] on [[https://en.wikipedia.org/wiki/Darwin_(operating_system)][Darwin]]:
This setup uses the following links to install [[https://go.dev/][Go]] and tools on [[https://en.wikipedia.org/wiki/Darwin_(operating_system)][Darwin]]:
1. [[https://go.dev/dl/][Go - all releases - featured downloads]] contains binary and source packages.
2. [[https://go.dev/doc/install][Go installation]] explains how to setup your system.
3. Install [[https://pkg.go.dev/golang.org/x/tools/gopls][gopls]] following the [[https://pkg.go.dev/golang.org/x/tools/gopls#section-readme][installation instructions in the GoPLS README]].
3. Install [[https://pkg.go.dev/golang.org/x/tools/gopls][gopls]], [[https://staticcheck.dev/][staticcheck]], and [[https://pkg.go.dev/golang.org/x/tools/cmd/goimports][goimports]] using the commands in listing
[[lst:install-go-tools]].
Links to learn [[https://go.dev/][Go]] are:
1. [[https://www.golangprograms.com/go-language.html][Go programming language]] contains a tutorial and a reference.
2. [[https://gobyexample.com/][Go by example]] is a showcase of short examples with explanations.
3. [[https://interpreterbook.com/][Writing An Interpreter In Go]] is book showing how to write a real program.
Listing [[lst:setup-go-1]] defines =gotsfmt= which is a cheap small replacement for
=gofmt= in [[https://github.com/dominikh/go-mode.el][go-mode]]. Adding =gofmt= to =before-save-hook= is safe, but adding
=gotsfmt= is unsafe because =gotsfmt= calls =save-buffer=. Listing
[[lst:setup-go-2]] defines =gots8tabs=, =gots4tabs=, and =gots2tabs= which set tab
stops to respectively 8, 4, and 2 spaces locally in the current buffer if the
major mode is =go-ts-mode=.
#+caption[Install =gopls= and =staticcheck=]:
#+caption: Install =gopls= and =staticcheck=.
#+name: lst:install-go-tools
#+begin_src shell :eval never :tangle no
go install golang.org/x/tools/gopls@latest
go install honnef.co/go/tools/cmd/staticcheck@latest
go install golang.org/x/tools/cmd/goimports@latest
#+end_src
The installed [[https://github.com/lassik/emacs-format-all-the-code][format-all for Emacs]] library supports all Go code formatting
tools. Listing [[lst:setup-go]] defines =gots8tabs=, =gots4tabs=, and =gots2tabs=
which set tab stops to respectively 8, 4, and 2 spaces locally in the current
buffer if the major mode is =go-ts-mode=.
#+caption[Setup tab stops handling]:
#+caption: Setup tab stops handling.
#+name: lst:setup-go-1
#+name: lst:setup-go
#+begin_src emacs-lisp -n :results silent
(when (featurep 'treesit)
(defun gots2tabs ()
"Set tab stops to 2 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 2)
(setq-local tab-width 2)))
(defun gots4tabs ()
"Set tab stops to 4 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 4)
(setq-local tab-width 4)))
(defun gots8tabs ()
"Set tab stops to 8 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 8)
(setq-local tab-width 8)))
(add-to-list 'auto-mode-alist `(,(rx ".go" eos) . go-ts-mode)))
#+end_src
** [[https://github.com/dominikh/go-mode.el/blob/602d73e22646b1b98b2eb97927fd426c0d1d2f92/go-mode.el#L1904][Elisp gofmt (in go-mode) replacement]] :noexport:
:PROPERTIES:
:CUSTOM_ID: sec:elisp-gofmt-replacement
:header-args:emacs-lisp: :tangle no
:END:
Listing [[lst:setup-go-2]] defines =gotsfmt= which is a cheap small replacement for
=gofmt= in [[https://github.com/dominikh/go-mode.el][go-mode]]. Adding =gofmt= to =before-save-hook= is safe, but adding
=gotsfmt= is unsafe because =gotsfmt= calls =save-buffer=.
#+caption[Setup tab stops handling]:
#+caption: Setup tab stops handling.
#+name: lst:setup-go-2
#+begin_src emacs-lisp
(when (featurep 'treesit)
(defun gotsfmt ()
@ -4485,35 +4532,6 @@ major mode is =go-ts-mode=.
(message "Your code chokes Gofmt!"))))))
#+end_src
#+caption[Setup tab stops handling]:
#+caption: Setup tab stops handling.
#+name: lst:setup-go-2
#+begin_src emacs-lisp -n :results silent
(when (featurep 'treesit)
(defun gots2tabs ()
"Set tab stops to 2 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 2)
(setq-local tab-width 2)))
(defun gots4tabs ()
"Set tab stops to 4 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 4)
(setq-local tab-width 4)))
(defun gots8tabs ()
"Set tab stops to 8 spaces in current `go-ts` buffer."
(interactive)
(when (eq major-mode 'go-ts-mode)
(setq-local go-ts-mode-indent-offset 8)
(setq-local tab-width 8)))
(add-to-list 'auto-mode-alist `(,(rx ".go" eos) . go-ts-mode)))
#+end_src
** [[https://www.seas.upenn.edu/~chaoliu/2017/09/01/python-programming-in-emacs/][Python programming]]
:PROPERTIES:
:CUSTOM_ID: sec:python-programming