Thursday, September 1, 2011

Using ctags with Go

I'm a Vim user, and using ctags with it help me quickly navigate through code. ctags does not come with support for Go (yet?). But it's easy to add new language support to it using custom regular expressions. The below code will detect function definition in Go files. Then after running ctags -R in your project root you can hit CTRL-[ in Vim to jump to function definition.

Add the following to ~/.ctags:


Oh, and you should check out gocode as well.

4 comments:

  1. Can you please tell me how to use it. When I typed the following at the root of my project.

    ctags --langdef=Go --langmap=Go:.go --regex-Go=/func([ \t]+\([^)]+\))?[ \t]+([a-zA-Z0-9_]+)/\2/d,func/ --regex-Go=/var[ \t]+([a-zA-Z_][a-zA-Z0-9_]+)/\1/d,var/ --regex-Go=/type[ \t]+([a-zA-Z_][a-zA-Z0-9_]+)/\1/d,type/ -R myproj.tags


    it gave errors.
    -bash: syntax error near unexpected token `('

    Highly appreciate your help. thanks

    ReplyDelete
  2. Never mind. I figured it out that the above lines has to go in ~/.ctags file

    ReplyDelete
  3. Thanks for posting the comment. I've added a line about ~/.ctags.

    ReplyDelete

/* MIKI: Analytics */