Emacs: Format Golang Code 🚀

By Xah Lee. Date: .

put this in your Emacs Init File:

(defun xah-go-format-buffer ()
  "Reformat current file by calling shell command gofmt.
This command saves the buffer first.

URL `http://xahlee.info/emacs/emacs/xah_format_golang_code.html'
Version: 2021-01-15 2021-11-27 2022-07-24 2022-08-28"
  (interactive)
  (let ((xfname (buffer-file-name)))
    (when xfname
      (when (buffer-modified-p) (save-buffer))
      (shell-command (format "gofmt -w %s" xfname))
      (revert-buffer t t t)
      )))

part of Emacs: Xah Go Mode (xah-go-mode.el)

Emacs, Format Code