关于golang:emacs-配置golang开发环境

246次阅读

共计 2524 个字符,预计需要花费 7 分钟才能阅读完成。

一、参考

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 区域

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

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

4.2 godef

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

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

4.3 function

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

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

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

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

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

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

4.4 其余

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

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

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

五、快捷键绑定

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-;能够实现多行正文与解正文

六、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 $

正文完
 0