2.2 KiB
Go
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software1
Setup
Install golang
1 on your system, and configure your environment prior to loading this module.
RUN apt install -y golang
Language server
Get started by installing the gopls
2 language server.
GO111MODULE=on go get golang.org/x/tools/gopls@latest
Setup the environment
Make some modifications to the environment. Set the $GOPATH
variable prior to loading, allowing modification of the default value. Include the bin
subdirectory of the $GOPATH
in the $PATH
variable, adding compiled golang applications to the system path.
(setenv "GOPATH" (concat (getenv "HOME") "/.go/")) (setenv "PATH" (concat (getenv "GOPATH") "bin:" (getenv "PATH")))
Config
Use the go-mode
3 package for integration with lsp-mode
, manually setting the path to gopls
2 before loading.
(use-package go-mode :hook (go-mode . lsp) :custom (lsp-go-gopls-server-path "~/.go/bin/gopls"))
Before save hooks
Apply some custom behaviour when saving golang
1 buffers. Format and organize the imports before saving.
(defun dotfiles/go-hook () (add-hook 'before-save-hook #'lsp-format-buffer t t) (add-hook 'before-save-hook #'lsp-organize-imports t t)) (add-hook 'go-mode-hook #'dotfiles/go-hook)
Babel structure templates
Configure the babel
engine to support golang
1 source blocks.
(add-to-list 'org-structure-template-alist '("go" . "src go"))