Emacs的安装与golang配置
安装 emacs
Window10
- 从https://www.gnu.org/software/emacs/ 下载最新的emacs版本, 并将zip解压到e:/emacs
- 双击 addpm.exe, 安装emacs
Centos
- 从https://www.gnu.org/software/emacs/ 下载最新的emacs版本,并将tar xvfz *.gz 解压缩
- 安装emacs依赖的系统库
yum -y groupinstall "Development Tools"
yum -y install gtk+-devel gtk2-devel
yum -y install libXpm-devel
yum -y install libpng-devel
yum -y install giflib-devel
yum -y install libtiff-devel libjpeg-devel
yum -y install ncurses-devel www.2cto.com
yum -y install gpm-devel dbus-devel dbus-glib-devel dbus-python
yum -y install GConf2-devel pkgconfig
yum -y install libXft-devel
./configure --prefix=/usr/local --with-x-toolkit=gtk
make && make install
配置.emacs
设置ELPA软件库
melpa库里有最新的软件,包括go-mode等, 同时设置自己的依赖包, 这样在打开emacs会自动检测此电脑是否有插件,没有就安装 安装以下依赖
* go-mode
* monokai-theme
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.org/packages/") t)
(package-initialize)
(defvar my-packages '(go-mode))
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
设置monokai theme
;; load the monokai theme
(load-theme 'monokai t)
ECB
ECB需要手动安装
M-x package-list-packages
;;安装完成后
M-x customize-variable 回车 desktop-save-mode 回车
M-x customize-variable 回车 ecb-auto-activate 回车
M-x customize-variable 回车 ecb-primary-secondary-mouse-buttons 回车
M-x ecb-store-window-sizes ;;保存当前ecb窗口大小
;;http://ecb.sourceforge.net/docs/Changing-the-ECB-layout.html
GO开发环境
参考 Emacs for Go
go-mode 配置与使用
;;C-c C-a 执行go-import-add命令(go-mode默认)
;;C-u C-c C-a 执行go-import-add 同时取别名
;;C-c C-r go-remove-unused-imports
;;C-c C-g go-goto-imports
;;C-c C-f gofmt
;; C-c C-k godoc
;; C-c C-d godef-describe (This requires you to install godef via go get github.com/rogpeppe/godef.)
;; C-c C-j godef-jump
;; C-x <LEFT> will take you back to the previous buffe
;; C-x 4 C-C C-j godef-jump-other-window
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-r") 'go-remove-unused-imports)))
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-g") 'go-goto-imports)))
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-f") 'gofmt)))
(add-hook 'go-mode-hook '(lambda ()
(local-set-key (kbd "C-c C-k") 'godoc)))
(add-hook 'before-save-hook 'gofmt-before-save)
;;godef 工具可以分析 go code, 同时允许你跳转到函数的定义或对像的定义的地方
;;安装godef 运行 go get code.google.com/p/rog-go/exp/cmd/godef
;; 将$GOPATH/bin/godef 复制到 $GOROOT/bin
go oracle
go oracle 是一个非官方的源代码分析工具, 它可以回答
What is the type of this expression? What are its methods?
What’s the value of this constant expression?
Where is the definition of this identifier?
What are the exported members of this imported package?
What are the free variables of the selected block of code?
What interfaces does this type satisfy?
Which concrete types in the package implement this interface?
And:
Where are the references to this identifier?
What are the possible concrete types of this interface value?
What are the possible callees of this dynamic call?
What are the possible callers of this function?
What objects might this pointer point to?
Where will a value sent on this channel be received?
Which statements could update this field/local/global/map/array/etc?
Which functions might be called indirectly from this one?
先下载 go get golang.org/x/tools/cmd/oracle, 将oracle copy to $GOROOT/bin
(load "$GOPATH/src/golang.org/x/tools/cmd/oracle/oracle.el")
(add-hook 'go-mode-hook 'go-oracle-mode)
;; 可以输入 M-x go-oracle-set-scope 看是否有此命令测试是否安装成功
在使用go-oracle之前,必须先设置分析的范围, 通过M-x go-oracle-set-scope 添加一个或多个packages 或者 import中包的路径. 最简单的是只有一个main package. 但这个工具的优势是,scope设置的超大超好。 packcage之间以空格分隔。 举一个例子, 我们分析的包为fmt. golang.org/x/tools/cmd/oracle/oracle.el 将会导入fmt包,并进行分析.
C-c C-o < go-oracle-callers C-c C-o > go-oracle-callees C-c C-o c go-oracle-peers C-c C-o d go-oracle-definition C-c C-o f go-oracle-freevars C-c C-o g go-oracle-callgraph C-c C-o i go-oracle-implements C-c C-o p go-oracle-pointsto C-c C-o r go-oracle-referrers C-c C-o s go-oracle-callstack C-c C-o t go-oracle-describe
Syntax/error checking
安装flycheck
M-x package-show-package-list [Enter] flycheck
company-go 自动完成代码
先下载 go get -u github.com/nsf/gocode 并将gocode.exe copyto GOROOT/bin
;;设置company-mode 在go-mode时,开启 (add-hook 'go-mode-hook 'company-mode) (add-hook 'go-mode-hook (lambda () (set (make-local-variable 'company-backends) '(company-go)) (company-mode)))
yasnippet安装
关闭备份文件
M-x customize-variable 回车 make-backup-files 回车