emacs-package-for-golang

ivy-mode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

;;
;; ivy mode
;;
(use-package ivy
:ensure t
:diminish (ivy-mode . "")
:config
(ivy-mode 1)
(setq ivy-use-virutal-buffers t)
(setq enable-recursive-minibuffers t)
(setq ivy-height 10)
(setq ivy-initial-inputs-alist nil)
(setq ivy-count-format "%d/%d")
(setq ivy-re-builders-alist
`((t . ivy--regex-ignore-order)))
)

;;
;; counsel
;;
(use-package counsel
:ensure t
:bind (("M-x" . counsel-M-x)
("\C-x \C-f" . counsel-find-file)))

;;
;; swiper
;;
(use-package swiper
:ensure t
:bind (("\C-s" . swiper))
)

gomode + goimport自动导入 + godef跳转

go get -u github.com/rogpeppe/godef
go get -u golang.org/x/tools/cmd/goimports

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(use-package go-mode
;; :load-path "~/.emacs.d/vendor/go-mode"
:mode ("\\.go\\'" . go-mode)
:ensure-system-package
((goimports . "go get -u golang.org/x/tools/cmd/goimports")
(godef . "go get -u github.com/rogpeppe/godef"))
:init
(setq gofmt-command "goimports"
indent-tabs-mode t)
:config
(add-hook 'before-save-hook 'gofmt-before-save)
:bind (:map go-mode-map
("\C-c \C-c" . compile)
("\C-c \C-g" . go-goto-imports)
("\C-c \C-k" . godoc)
("M-j" . godef-jump)))

M-n find引用 M-x lsp-find-ref
M-. find 实现

gocode+company 自动补全

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;;
;; company
;;
(use-package company
:ensure t
:config
(global-company-mode t)
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 3)
(setq company-backends
'((company-files
company-yasnippet
company-keywords
company-capf
)
(company-abbrev company-dabbrev))))

(add-hook 'emacs-lisp-mode-hook (lambda ()
(add-to-list (make-local-variable 'company-backends)
'(company-elisp))))

gocode
go get -u github.com/stamblerre/gocode # for go-eldoc/company-go // this feel better than nsf/gocde

gocode 是守护进程,查看是否在后台运行

1
ps -e | grep gocode

语法检查

C-c ! l
C-c ! v

1
2
3
4
5
(use-package flycheck
:ensure t
:config
(global-flycheck-mode t)
)

gotest

1
2
3
4
5
6
7
8
9
10
(use-package gotest
:after go-mode
:bind (:map go-mode-map
("C-c C-f" . go-test-current-file)
("C-c C-t" . go-test-current-test)
("C-c C-p" . go-test-current-project)
("C-c C-b" . go-test-current-benchmark)
("C-x x" . go-run))
:config
(setq go-test-verbose t))

gorename

go get -u golang.org/x/tools/cmd/gorename

1
2
3
4
5
(use-package go-rename
:after go-mode
:ensure-system-package (gorename . "go get -u golang.org/x/tools/cmd/gorename")
:bind (:map go-mode-map
("C-c C-r" . go-rename)))

errorcheck

go get -u github.com/kisielk/errcheck

1
2
3
4
5
(use-package go-errcheck
:after go-mode
:ensure-system-package (errcheck . "go get -u github.com/kisielk/errcheck")
:bind (:map go-mode-map
("C-c C-e" . go-errcheck)))

go-imenu

go get -u github.com/lukehoban/go-outline

1
2
3
4
5
(use-package go-imenu
:ensure-system-package (go-outline . "go get -u github.com/lukehoban/go-outline")
:after go-mode
:config
(add-hook 'go-mode-hook 'go-imenu-setup))

gtags

安装GUN global though source code 注意不要用brew安装,因为必须要配置sqlite3

1
2
3
4
5
6
wget http://tamacom.com/global/global-6.5.7.tar.gz
tar xvf global-6.5.7.tar.gz
cd global-6.5.7
./configure --with-sqlite3
make
sudo make install
1
go get github.com/juntaki/gogtags

在项目目录中输入:
gogtags -v
会生成GTAGS等文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(use-package helm-gtags
:config
(setq helm-gtags-ignore-case t
helm-gtags-auto-update t
helm-gtags-use-input-at-cursor t
helm-gtags-pulse-at-cursor t
helm-gtags-prefix-key "\C-cg"
helm-gtags-suggested-key-mapping t)
:bind (:map helm-gtags-mode-map
("C-c g a" . helm-gtags-tags-in-this-function)
("C-j" . helm-gtags-select)
("M-." . helm-gtags-dwim)
("M-," . helm-gtags-pop-stack)
("C-c <" . helm-gtags-previous-history)
("C-c >" . helm-gtags-next-history))
:hook ((dired-mode eshell-mode c-mode c++-mode asm-mode) . helm-gtags-mode))

这时候再项目中即可使用
M-x helm-gtags finn partern 等查找tags

projecttle 在项目中切换

1
2
3
4
5
6
7
8
9
10
11
(use-package dumb-jump
:bind (("M-g o" . dumb-jump-go-other-window)
("M-g j" . dumb-jump-go)
("M-g x" . dumb-jump-go-prefer-external)
("M-g z" . dumb-jump-go-prefer-external-other-window))
:config
;; (setq dumb-jump-selector 'ivy) ;; (setq dumb-jump-selector 'helm)
:initny
(dumb-jump-mode)
:ensure
)

dumb-jump 跳转OB

1
2
3
4
5
6
7
8
9
10
11
(use-package dumb-jump
:bind (("M-g o" . dumb-jump-go-other-window)
("M-g j" . dumb-jump-go)
("M-g x" . dumb-jump-go-prefer-external)
("M-g z" . dumb-jump-go-prefer-external-other-window))
:config
;; (setq dumb-jump-selector 'ivy) ;; (setq dumb-jump-selector 'helm)
:init
(dumb-jump-mode)
:ensure
)