Should work with go mod projects.
Most of the heavy lifting should be lsp
and gopls
This is based on how spacemacs go layer was structured, however key elements are changing for go mod support. Hence this is going to be in flux.
(use-package go-mode
:ensure t
:bind (
;; If you want to switch existing go-mode bindings to use lsp-mode/gopls instead
;; uncomment the following lines
;; ("C-c C-j" . lsp-find-definition)
;; ("C-c C-d" . lsp-describe-thing-at-point)
)
:config
; Use goimports instead of go-fmt
(setq gofmt-command "goimports")
(add-hook 'go-mode-hook 'company-mode)
;; Call Gofmt before saving
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook 'setup-go-mode-compile)
(add-hook 'go-mode-hook #'smartparens-mode)
(add-hook 'go-mode-hook 'yas-minor-mode)
(add-hook 'go-mode-hook 'flycheck-mode)
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-r") 'go-remove-unused-imports)
(local-set-key (kbd "C-c b") 'go-run)))
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-g") 'go-goto-imports)))
(add-hook 'go-mode-hook (lambda ()
(set (make-local-variable 'company-backends) '(company-go))
(company-mode)))
:hook ((go-mode . lsp-deferred)
(before-save . lsp-organize-imports)))
(use-package gotest
:ensure t
:config
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c a") 'go-test-current-project)
(local-set-key (kbd "C-c m") 'go-test-current-file)
(local-set-key (kbd "C-c ,") 'go-test-current-test)
))
)
(use-package company-go
:ensure t
:after go-mode
:config
(setq tab-width 4)
:bind (:map go-mode-map
; Godef jump key binding
("M-." . godef-jump)))
(use-package go-eldoc
:ensure t
:config
(add-hook 'go-mode-hook 'go-eldoc-setup))
(use-package go-guru
:ensure t)
(defun setup-go-mode-compile ()
; Customize compile command to run go build
(if (not (string-match "go" compile-command))
(set (make-local-variable 'compile-command)
"go build -v && go test -v && go vet")))
(use-package flycheck-golangci-lint
:ensure t
:config
(add-hook 'flycheck-mode-hook #'flycheck-golangci-lint-setup))
(use-package go-snippets
:ensure t)
(use-package ob-go
:ensure t)
(provide 'go)