Emacs for Go

Source: Internet
Author: User
Tags gocode
This is a creation in Article, where the information may have evolved or changed.

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 would probably be updated as I discover new modes and tools.

The Emacs modes I currently use is: go-mode, Oracle, Goflymake, and Gocode.

I break out my Emacs configuration to language or package specific configuration files. This keeps ~/emacs.d/init.el pretty lean as all it is doing are loading in other configuration files with calls to load lik E 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 Remov E imports, the ability to browse documentation and 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'll prompt you to provive an alternate name for the import. to does this type  c-u c-c c-a . If No list of imports exists Go-import-add would create one. If the import is adding was currently in the list of imports but commented out it'll be uncommented. Pretty good stuff for a very common pattern. One point to keep on mind is that  go-import-add  will The only work once the a package declaration in P Lace.

Go-remove-unused-imports, as the name implies, removes all unused imports. As withGo-import-add calling go-removed-unused-imports with a prefix would 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-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 the 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 would move the point to the imports block. By default it is ' 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 both ways: c-c c-f as a mnemonic for ' format ' allowing me to format the buffer without SA Ving Tsun and also with a "before-save-hook" so that the buffer was 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 is 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  Apropos Godoc  will Use whatever token was under the point as the search term but would promt 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, parses go code and enables you to quickly jump, the definition of any symbol or Read its description. As with Godoc, Godef must is installed and on your $PATH. To install Godef run: go get code.google.com/p/rog-go/exp/cmd/godef. Go-mode provides-functions for interacting with godef: godef-describe and godef-jump< /c6>.

godef-describe  is insanely useful and would give you information about the symbol "re looking at. By default is bound to  c-c c-d . If the point was on a function call Godef-describe would show its full definition (parameter name and type, return Ty PE). If the point was on a struct  godef-describe  will Show all members of the struct.

Godef-jump 'll jump the defintion of the expression at the point. It's bound to c-c c-j. Remember that c-x <LEFT> would take your back to the previous buffer once your ' re done reading the definition of whatever you ' re looking at.

Go Oracle

go Oracle  is an external source analysis tool, which let's you answer questions like: "What interfaces does T His type implement? "," which types implement the interface? "," Who's listening for events on this channel? " He possible callers of this function? " ... etc... I highly recommend reading the  go Oracle documentation in depth-it is a advanced tool and I ' M only SC Ratching the surface here. As the documentation states, this tool was a work in progress. Rough edges is expected but the benefits is well worth it.

to install  go-oracle  run:  go get code.google.com/p/go.tools/cmd/oracle   Which'll 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 usinggo-oracleYou must set the ' scope of analysis '. This is do by calling theGo-oracle-set-scopefunctionm-x Go-oracle-set-scopeand 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 is better since the tool would analyze more code-but it might also slow down the Oracl e a bit. If you need to specify multiple packages simply separate them with a space. A example, from the documentation, for analysis scope is:FMT code.google.com/p/go.tools/cmd/oraclewhich includes theFMTPackage and also thego-oraclePackage 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 Cal LS this function. To see the possible sends/receives in 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-callers' no work in Mac OS X so you might need to r Ebind them.

C-c C-o < go-oracle-callersc-c c-o > go-oracle-calleesc-c c-o c go-oracle-peersc-c c-o D go- Oracle-definitionc-c c-o F go-oracle-freevarsc-c c-o g go-oracle-callgraphc-c c-o i go-oracle-implements C-c C-o P go-oracle-pointstoc-c c-o r go-oracle-referrersc-c c-o s go-oracle-callstackc-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 and the location of the 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 is highlighted as a 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/gocode .   Gocode &NBSP;ALSP depends on either  auto-complete-mode  or company-mode . I went with  company-mode  and  company-mode-go . Both packages is available in Elpa so simply run:  m-x package-install RET company-go

Next we need to automatically load company-mode whenever Go-mode are loaded and also configure Compan Y-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 as the following (after searching for "Get "). Notice the function definition in the Minibuffer.

That's it for now. Happy hacking.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.