emacs 配置golang开发环境

一、参考

Becoming an emacs gopher

Emacs Golang开发环境配置指南

Stop debugging Go with Println and use Delve instead

Delve调试器

二、安装golang包

2.1 goimports

Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones.

对于代码中引入的第三方模块,自动更新

goimports命令能自动格式化代码,自动添加、移除imports,而且与Emacs集成良好。可以替代官方gofmt命令。

GO111MODULE=on go get -u golang.org/x/tools/cmd/goimports

2.2 godef

Godef, given an expression or a location in a source file, prints the location of the definition of the symbol referred to.

对于代码中的函数引用,可以查看原始定义位置
godef命令能在Go源码变量、函数定义间跳转,是查看变量、函数、文件定义的好助手

GO111MODULE=on go get -u github.com/rogpeppe/godef

2.3 gocode

An autocompletion daemon for the Go programming language

自动补全代码
gocode命令能为代码自动补全提供后台支持,是EmacsGo代码补全必不可少的backend

GO111MODULE=on go get -u github.com/nsf/gocode

三、安装emacs包

包名 描述 其他
auto-Complete An Intelligent auto-completion extension for Emacs https://github.com/auto-compl...
go-mode Emacs mode for the Go programming language https://github.com/dominikh/g...
go-complete Native Go completion for Emacs https://github.com/vibhavp/go...
go-autocomplete auto-complete-mode backend for go-mode https://melpa.org/#/go-autoco...
autopair Automagically pair braces and quotes in emacs like TextMate https://github.com/joaotavora...
;; Init the auto complete modules
(ac-config-default)
(global-auto-complete-mode t)
(require 'go-autocomplete)

;; Enable auto-complete
(auto-complete-mode 1)

;; Define keymaps
(define-key ac-mode-map (kbd "M-TAB") 'auto-complete)
(global-set-key (kbd"C-c C-c") 'godef-jump)

;; Set some quick config vals
(setq ac-auto-start 1)
(setq ac-auto-show-menu 0.8)

;; Just to make sure go tools are enabled
(add-to-list 'exec-path "~/go/bin")

;; Automatically format code on save
(setq gofmt-command "goimports")
(add-hook 'before-save-hook 'gofmt-before-save)
(add-hook 'go-mode-hook 'auto-complete-for-go)

四、go-mode的命令

4.1 imports区域

(1) go-goto-imports, 跳转到imports区域

image.png

(2) go-import-add, 添加新的import aaa

image.png

(3) go-remove-unused-imports, 删除不再使用的包

image.png

4.2 godef

(1) go-jump,跳转到表达式的定义

image.png

(2) godef-jump-other-window,新建窗口,并跳转到表达式定义

image.png

4.3 function

(1) go-goto-arguments, 跳转到函数的参数

image.png

(2) go-goto-docstring 跳转到函数的说明文档,如果没有说明文档,会创建

image.png

(3) go-goto-function, 跳转到函数func关键字,一般用于在函数文档中,跳转到函数代码

image.png

(4) go-goto-function-name, 跳转到函数名称处

image.png

(5) go-goto-return-values, 跳转到函数定义中的返回值处

image.png

(6) go-goto-method-receiver, 跳转到函数的调用者处

image.png

4.4 其他

(1) pop-tag-mark, 回退到前一个光标位置

image.png

(2) beginning-of-defun, 跳转到函数的初始位置(适合函数之间的跳转)

image.png

(3) end-of-defun, 跳转到函数的结束位置

image.png

五、快捷键绑定

5.1 函数跳转和回退

;; Godef jump key binding                                                                   
(global-set-key (kbd "M-,") 'godef-jump)                                                  
(global-set-key (kbd "M-.") 'pop-tag-mark)

5.2 代码块的跳转

C-M-a 跳转到上一个函数

C-M-e 跳转到下一个函数

5.3 代码注释

M-;可以实现多行注释与解注释

image.png

六、debug

6.1 安装

GO111MODULE=on go get -u github.com/go-delve/delve/cmd/dlv

➜  ~ dlv version
Delve Debugger
Version: 1.6.0
Build: $Id: 8cc9751909843dd55a46e8ea2a561544f70db34d $

你可能感兴趣的:(golangemacs)