Emacs for Go

In this post I‘m going to explore customizing Emacs to make hacking on Go a little more fluid. This is a living document and will probably be updated as I discover new modes and tools.

The Emacs modes I currently use are: go-mode, oracle, goflymake, and gocode.

I break out my Emacs configuration into language or package specific configuration files. This keeps ~/emacs.d/init.el pretty lean as all it is doing is loading in other configuration files with calls to load like this: (load "~/.emacs.d/go.el").

go-mode

go-mode is the first mode we‘ll look at. It provides syntax highlighting, default indentation matching the golang standards and gofmt, the ability to add and remove imports, the ability to browse documentation and support for integrating godoc and godef.

To enable go-mode add the following go.el:

(require ‘go-mode)(add-hook ‘before-save-hook ‘gofmt-before-save)

Let‘s start by looking at arguably the most fundamental capability added by go-mode - managing imports. go-mode adds three functions of note: go-import-add, go-remove-unused-imports and go-goto-imports.

go-import-add is bound to C-c C-a by default. If you call go-import-add with a prefix it will prompt you to provive an alternate name for the import. To do this type C-u C-c C-a. If no list of imports exists go-import-add will create one. If the import you are adding is currently in the list of imports but commented out it will be uncommented. Pretty good stuff for a very common pattern. One point to keep in mind is that go-import-add will only work once you have a package declaration in place.

go-remove-unused-imports, as the name implies, removes all unused imports. As withgo-import-add calling go-removed-unused-imports with a prefix will change the default behavior - commenting out unused imports instead of removing them. By default isn‘t bound to anything by default. So let‘s start by setting up a keybinding specific to go-mode in ~/.emacs.d/go.el

(add-hook ‘go-mode-hook ‘(lambda ()
  (local-set-key (kbd "C-c C-r") ‘go-remove-unused-imports)))

Now we can use C-c C-r to remove all unused imports, or C-u C-c C-r to comment out unused imports.

go-goto-imports is a pretty nifty helper function that will move the point to the imports block. By default it isn‘t bound to any keys by default so let‘s hop back into~/.emacs.d/go.el and add a keybinding for it. I set it to C-c C-g as a mnemonic of "goto"

(add-hook ‘go-mode-hook ‘(lambda ()
  (local-set-key (kbd "C-c C-g") ‘go-goto-imports)))

gofmt formats the current buffer according to the gofmt rules. By default it isn‘t bound to anything. I bind gofmt in two ways: C-c C-f as a mnemonic for "format" allowing me to format the buffer without saving and also with a "before-save-hook" so that the buffer is always correctly formatted when I save.

(add-hook ‘go-mode-hook ‘(lambda ()
  (local-set-key (kbd "C-c C-f") ‘gofmt)))(add-hook ‘before-save-hook ‘gofmt-before-save)

godoc shows the go documentation for a given package. Note, godoc depends on the godoc utility. It must be installed and on your $PATH. To install it run: go get code.google.com/p/go.tools/cmd/godoc. By default godoc isn‘t bound to anything. I map this to C-c C-k as a mnemonic for man -k or aproposgodoc will use whatever token is under the point as the search term but will promt you if you want to search godoc for something else. Try moving to one of your imports and hitting ‘C-c C-k‘.

(add-hook ‘go-mode-hook ‘(lambda ()
  (local-set-key (kbd "C-c C-k") ‘godoc)))

godef is a really nifty tool that parses go code and enables you to quickly jump the definition of any symbol or read its description. As with godoc, godef must be installed and on your $PATH. To install godef run: go get code.google.com/p/rog-go/exp/cmd/godefgo-mode provides two functions for interacting with godefgodef-describe and godef-jump.

godef-describe is insanely useful and will give you information about the symbol you‘re looking at. By default is is bound to C-c C-d. If the point is on a function callgodef-describewill show its full definition (parameter name and type, return type). If the point is on a struct godef-describe will show all members of the struct.

godef-jump will jump the defintion of the expression at the point. It is bound to C-c C-j. Remember that C-x <LEFT> will take you back to the previous buffer once you‘re done reading the definition of whatever you‘re looking at.

go oracle

go oracle is an external source analysis tool that let‘s you answer questions like: "what interfaces does this type implement?", "which types implement this interface?", "who is listening for events on this channel?", "what are the possible callers of this function?"...etc. I highly recommend reading the go oracledocumentation in depth - it is an advanced tool and I‘m only scratching the surface here. As the documentation states, this tool is a work in progress. Rough edges are expected but the benefits are well worth it.

To install go-oracle run: go get code.google.com/p/go.tools/cmd/oracle which will install the tool in your $GOPATH/bin directory. However, the emacs bindings expect to find go-oracle in $GOROOT/bin and not $GOPATH/bin so you‘ll need to manually move the binary to the right place: mv $GOPATH/bin/oracle $GOROOT/bin/. Next we‘ll need to configure emacs to load the bindings. Add something like the following to go.el -- obviously with $GOPATH matching your own $GOPATH. The second line ensures go-oracle-mode is enabled whenever editing go code.

(load "$GOPATH/src/code.google.com/p/go.tools/cmd/oracle/oracle.el")(add-hook ‘go-mode-hook ‘go-oracle-mode)

Before using go-oracle you must set the ‘scope of analysis‘. This is done by calling the go-oracle-set-scope function M-x go-oracle-set-scope and listing one or more packages or import paths of packages. In the simplest case this is the ‘main‘ package of your go application. As the tool states, bigger scopes are better since the tool will analyze more code - but it might also slow down the oracle a bit. If you need to specify multiple packages simply separate them with a space. An example, from the documentation, of an analysis scope is: fmt code.google.com/p/go.tools/cmd/oracle which includes the fmt package and also thego-oracle package for analysis.

To start getting a feel for go-oracle move the point to a function and enter C-c C-o <to see what calls this function. To see the possible sends/receives on a channel move the point to the channel and run C-c C-o c.By default go-oracle-mode sets up the following keybinds. Note: keybindings with ‘<‘ and ‘>‘ such as go-oracle-callersmay not work on Mac OS X so you might need to rebind them.

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

goflymake

goflymake is a wrapper around the go tool providing flymake compatible syntax checking for go source. Since goflymake is actually compiling the source we get full compile and link checking as well.

To install the goflymake wrapper run go get -u github.com/dougm/goflymake. Next we‘ll need to configure emacs. Add the following to go.el obviously replacing $GOPATH with the location of your go workspace.

(add-to-list ‘load-path "$GOPATH/src/github.com/dougm/goflymake")(require ‘go-flymake‘)

The error checking provided by goflymake looks like the following. Note: unused imports are highlighted as an error, as is the incorrect call to http.Get()

gocode

gocode provides context-sensitive autocompletion. To install gocode run: go get -u github.com/nsf/gocodegocode alsp depends on either auto-complete-mode orcompany-mode. I went with company-mode and company-mode-go. Both packages are available in ELPA so simply run: M-x package-install RET company-go

Next we need to automatically load company-mode whenever go-mode is loaded and also configure company-go as its backend. Add the following to go.el.

(add-hook ‘go-mode-hook ‘company-mode)(add-hook ‘go-mode-hook (lambda ()  (set (make-local-variable ‘company-backends) ‘(company-go))
  (company-mode)))

The auto-completion provided by gocode and company-go looks like the following (after searching for "Get"). Notice the function definition in the minibuffer.

That‘s it for now. Happy hacking.

时间: 2024-08-06 16:00:54

Emacs for Go的相关文章

eclipse下的emacs风格快捷键

Ieclipse emacs类快捷键 win + shift + b 切换设置断点 win + shift + f 格式化代码 win + shift + l 显示绑定的快捷键 win + shift + s 保存所有文件 win + shift + w 关闭所有打开的文件 win + shift + p 跳到匹配括号处 win + shift + f11 运行文件 win + shift + / 搜索 win + alt + n 新建项目或文件 win + alt + r 同时编辑同名的所有变

我的emacs设置文件.emacs

;;; emacs配置文件 ;;设置行高(setq-default line-spacing 5) (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there

Vim 和 Emacs 文本编辑器:你更喜欢哪个?

关于Linux的学习,请参考书籍<Linux就该这么学> 这可能是2016年,在许多开发人员和系统管理员之间,编辑器战争中仍然活得很好的. VIM(vi)或Emacs:两者都有很强的功能,当然,很多人使用别的文本编辑器,你喜欢哪个? 毫无疑问,这两个还是有很强的追随者:当我们被问及最喜欢的文本编辑器,自从去年vi出世,但很显然,辩论仍然很激烈. 哪个最适合你,可能很大取决于个人的喜好.你可能已经内置插件满足您的需求,也许在你开发的同时产生了一些强烈的感情. 因此,让我们知道你喜欢哪种文本编辑器

Emacs

一.简介 Emacs,著名的集成开发环境和文本编辑器.Emacs与vim并立,被公认为是最受专业程序员喜爱的代码编辑器之一.   二.基础教程 1)Emacs快捷键 http://www.zzbaike.com/wiki/Emacs/Emacs%E5%BF%AB%E6%8D%B7%E9%94%AE#.E7.AA.97.E5.8F.A3.E6.93.8D.E4.BD.9C 2)成为Emacs高手 http://blog.csdn.net/redguardtoo/article/details/72

emacs ido模式

EMACS这类大神级的编辑器,欠缺一个左树右文件浏览模式的东西.经过一番调查发现,有dirtree能够搞定这个事情,另外还有几个其他的也能办到,例如speedbar, neotree都类似与vi 的nerdtree. 这些东东虽然好看,但是却不是emacs亲生的,都需要繁琐的配置.在stackoverflow中发现一个人推荐用ido,这个插件,原因是真正的程序员要做到眼中无树,人树合一的地步,推荐IDO只用两行代码就能搞定安装使用了.下面是步骤: 1,安装 (require 'ido) (ido

OSX 安装MacTex Emacs+Auctex

从stackoverflow上找到的,已经安装成功,借此留存下 Here's how I got Auctex to work with pure Emacs: Install the MacTeX distribution package Download OS X emacs, place in /Applications Download the Auctex tarball Go to the Auctex directory, configure with the command: .

Emacs 安装 jedi

Jedi 是个很棒的 python 的自动补全插件,可以显示 docstring, function arguments and code location. 安装步骤: 一.安装 python 的虚拟环境 sudo apt-get install python-virtualenv 或 sudo apt-get install python-pip sudo pip install virtualenv 二.安装 jedi 在emacs里操作(一个朋友的帮助,干脆利落): 1. M-x: li

emacs org-mode 常用命令

Table of Contents 1. orgguide 1.1. introduction 1.2. Document Structure   实用 1.3. Tables 1.4. Hyperlinks 1.5. Todo itesm 1.6. Tags 1.7. Properties 1.8. Dates and Times 1.9. Markup for rich exprot 1.10. Working with source code 2. emacs基本操作 2.1. emacs

Python环境搭建以及编译器Emacs

对于简明Python 的P13页,对shell和终端有疑问,现在来阐述两个概念: 所谓终端 终端本身是不会解析命令,它只是一个界面,是负责人机交互的一个接口.真正处理命令的并不是这些终端,真正处理命令行的是shell.终端只是负责提供一个输入命令的交互界面而已,在里面运行的命令并不归终端界面去解析,而是找到专门的命令行程序,这种程序我们一般将其称之为shell. 所谓shell shell是一个程序,一个二进制可运行可执行的程序,shell广义上可以指操作系统和用户接口的界面,图形界面也是一种s

emacs第一天

emacsbinw64.sourceforge.net  windows的emacs下载地方(绿色软件) 学习快速入门 C-h t 快速入门的帮助文档 光标移动快捷键: C-f 前进一格C-b 后退一格C-n 向下一行C-p 向上一行C-a 行开头C-e 行结尾C-k 删除当前位置到行尾查阅一下win上修改快捷键   这些快捷键在mac里面是全局的快捷键 注意修改:caps Lock 到ctrl C-g 中断命令(不想继续这个指令) s 代表 super键 S 代表 shift键 M-x lin