Configuration of golang module in space MCS

Posted by zeeshan_haider000 on Wed, 11 Sep 2019 06:40:29 +0200

Summary

Goang's official package management has been supported since version 1.11. Several attempts have been made before, but the results are not satisfactory, and dep has been used to manage the package.

With the release of version 1.13 recently and the increasing number of official management methods using go module, dep has not been developed for a long time. In view of this, it is time to migrate golang's package management mode to go module.

I have been using spacemacs to develop various applications, so this article also describes how to configure the development environment of golang module in spacemacs.

Configuration method

The go module itself does not need any configuration. The configuration here is actually a space Macs configuration for more convenient coding. It can support automatic completion, code inversion and other commonly used operations in development.

Required software

With go module, golang's project does not need to be placed under GOPATH. All traditional gocodes can no longer be used to complete these codes. Here we use gopls instead of gocode.

Goldang 1.13, the extra software needed is gopls

Installing golang 1.13, installing gopls is very simple:

$ GO111MODULE=on go get golang.org/x/tools/gopls@latest

Configuration of spacemacs

go layer

The develop ment branch of spacemacs inherits the function of lsp by default. What is LSP So it's easy to configure, under dotspace macs-configuration-layers:

dotspacemacs-configuration-layers
'(
  ... ...
  lsp
  ;; ----------------------------------------------------------------
  ;; Example of useful layers you may want to use right away.
  ;; Uncomment some layer names and press `SPC f e R' (Vim style) or
  ;; `M-m f e R' (Emacs style) to install them.
  ;; ----------------------------------------------------------------

  (go :variables
      go-backend 'lsp
      go-tab-width 8
      godoc-at-point-function 'godoc-gogetdoc)
 ... ...
 )

lsp-mode

lsp-mode It is the encapsulation of lsp by emacs. Referring to golang, it is configured in dotspace macs/user-config:

;; lsp
(use-package lsp-mode
  :hook (go-mode . lsp-deferred)
  :commands (lsp lsp-deferred))

(setq lsp-auto-guess-root nil)
(setq lsp-ui-doc-enable nil)
(setq lsp-ui-sideline-enable nil)
(setq lsp-prefer-flymake :none)

project root settings

After using go module, the directory of golang project is not necessarily under GOPATH, so a function to configure project root is defined. Of course, if not set, the default value is projectile-project-root of emacs.

;; project path settings
(setq current-project-path (projectile-project-root))

(defun set-project-path (relative-path)
  (interactive
   (list (read-string "relative path: " "." nil nil nil)))
  (setq current-project-path (file-truename relative-path)))

(defun get-project-path ()
  (interactive)
  (message current-project-path))

build/install shortcut key

Add 2 shortcuts to execute go build and go install

;; go build/install
(setq default-go-package "")

(defun go-build (&optional pkg)
  (interactive
   (list (read-string (format "Package Name[%s]: " default-go-package) nil nil "")))

  (if (not (string= pkg ""))
      (setq default-go-package pkg))

  (if (string= current-project-path "")
      (message "You MUST set current-project-path FIRST!")
    (projectile-with-default-dir current-project-path
      (projectile-run-compilation (concat "go build " default-go-package))))
  )

(defun go-install (&optional pkg)
  (interactive
   (list (read-string (format "Package Name[%s]: " default-go-package) nil nil "")))

  (if (not (string= pkg ""))
      (setq default-go-package pkg))

  (if (string= current-project-path "")
      (message "You MUST set current-project-path FIRST!")
    (projectile-with-default-dir current-project-path
      (projectile-run-compilation (concat "go install " default-go-package))))
)

;; set shortcuts
(spacemacs/set-leader-keys-for-major-mode 'go-mode
  "xi" 'go-install)

(spacemacs/set-leader-keys-for-major-mode 'go-mode
  "xb" 'go-build)

The shortcuts to go build and go install are SPC m x b and SPC MX i, respectively.

Pits encountered in use

In the process of trying to use, two pits were encountered, which took half a day. :

Error Content: LSP: main.go not in project or it is blacklist

In spacemacs **** Message ** buffer, prompt LSP: main. go not in the project or it is blacklisted nearly twists and turns, only to find out when the current project was added to the blacklist.

Solution:

  1. M-X lsp-workspace-blacklist-remove
  2. Find the folder where the project is located, select it, and remove it from the blacklist.

Error Content: no AST for file:///..../foo.go

If the project itself is a golang project, this problem will not generally arise. I have a project, golang project is one part of it. The structure of the project is roughly as follows:

.
├── backend
│   └── this-is-golang-project-root
├── deploy
│   ├── builder
│   └── docker-compose.yml
├── frontend
│   └── ui
└── README.md

The golang project is not in the root directory of the project. When it is used in ** Message ** buffer of spacemacs, no AST for file:///..../foo.go

Solutions:

  1. Setting (setq lsp-auto-guess-root nil)
  2. M-x set-project-path
  3. Setting up the golang project path is to use SPC m x b and SPC m x i to execute go build and go install at any time

Topics: Go vim Docker