goplexian.com - 2010-01-10 06:54:00
Tool creation is, to me, one of the most enjoyable things about knowing how to program, but unfortunately it can also lead to a huge waste of time if you let it get out of control.
The reason is because while it is true that a good environment can make writing code faster and easier, the speed gain is offset by the amount of time which it takes for you to build and maintain the environment.
This past week I finally got around to doing some of that maintenance to Emacs with regards to writing Go programs, and now, I am happy to pass that time investment on to you by sharing the results of my tinkering.
Allow me to introduce Tago,
Tago is Emacs TAGS for Go.
You may already be aware of a program called ctags for Vim
[1] or its Emacs equivalent called etags.
What etags does is parse a list of source files and generates a TAGS file which is an index of all the objects, functions, and variables in the source files. Emacs then uses this TAGS file in a couple of interesting ways, first of all it uses it to quickly find and jump to tags (functions, variables) in the source code where they are defined. Second it can be used to complete tag names as you are writing, sort of like a weak intelisense.
Such a tool is great for large code bases like the Go package libraries, but the problem is that etags does not support Go and it is written in C.
Tago is my replacement for etags, it generates an Emacs style TAGS file for Go source code, and it is written in Go.
Please check the included README file for instructions on compiling and usage.
After installing Tago you probably want to generate a TAGs file for the Go package libraries.
To do so just follow these three steps:
FIRST := Generate
Use the "find" utility and "xargs" to recursively search all of the Go pkg directory's and send a list of all the Go files to Tago, Tago will then write a TAGS file in the currect directory.
$> find /complete/path/go/src/pkg -name *.go | xargs tago
SECOND := Load
Tell Emacs to load the TAGS file
Emacs cmd: <M+x> visit-tags-table <RET> /complete/path/to/TAGS <RET>
THIRD := Use
You now have access to tag-completion!
Type something: "fmt."
Use Emacs cmd: <M+TAB>
Or alternative cmd: <M+x> complete-tag
Result: "fmt.Printf"
Continue to press <M+TAB> to cycle through alternative completions.
You also have access to tag-finding!
Type something: "ProbablyPrime"
Use Emacs cmd: <M+.>
Or alternative cmd: <M+x> find-tag
Result: The file "int.go" will open on line 359.
Emacs cmd: <M+*> // Will send you back to the previous file.
Emacs cmd: <M+x> find-tag-other-window // Will display the tag in a second window
Tago is, of course, also for creating TAGS for your own Go source code, simply run `tago myfile.go` or `tago *.go` and then add the resulting TAGS file to Emacs as explained in step #2.
This is one of those things that once you have and learn to use you will find to be indispensable, and of course all of these Emacs commands can be bound to other key sequences if you prefer.
I originally planned to explain how to change these key sequences and how to modify go-mode with hooks, but I'm going to put that off until next week.
Now in conclusion I am going to make a completely shameless attempt at earning a little cash from you, that's right you heard me, so if such a thing offends you then you should stop reading immediately:
I own the O'Reilly book "Learning GNU Emacs" and so I can personally recommend it as a great book for learning Emacs. It has gotten many positive reviews over the years and is now in its third edition but by far the absolute best part about this book is that if you
click on this link
and buy it then Amazon will give me a small slice of their profits!
I highly recommend all books with this feature.
Thanks for the clicks, goodnight!
[1]Michael Elkins wrote "gotags" for Vim, but it seems the link to that project no longer works.