Super charge your sublime text for #golang development

I have tried many things to set up my sublime text 3 environment, and I came up with the following configuration. It allows me to do:

  • Goto definition/declaration with short keys or mouse click
  • Auto completion even in complex situations
  • Context aware refactoring anything
  • Automatically lint and vet my codes and other helper tools
  • And more…

These are the tools you need for this set up to function:

$ go get -u github.com/nsf/gocode
$ go get -u golang.org/x/tools/cmd/guru
$ go get -u golang.org/x/tools/cmd/goimports
$ go get -u golang.org/x/tools/cmd/gorename
$ go get -u github.com/golang/lint/golint

I have installed gosublime which is an awsome plugin. Although I don’t rely on its goto definition functionality because it’s a bit slow and doesn’t work all the times. I only use it for code completion (with help of gocode) and triggering linters. Just install gosublime with Package Control and set it up. Here is my setup for gosublime:

{
"env": {
"GOPATH": "/home/arsham/Projects/Go:/usr/bin/go",
"PATH": "/home/arsham/Projects/Go/bin:$PATH:/usr/bin",
},
"autocomplete_suggest_imports": true,
"comp_lint_enabled": true,
"fmt_cmd": ["gofmt", "-s"],
"on_save": [
{"cmd": "gs_comp_lint"},
{"cmd": "go install"},
],
"comp_lint_commands": [
{"cmd": ["goimports -w $_fn"] , "shell": true},
{"cmd": ["golint $_fn"], "shell": true},
{"cmd": ["go", "vet"]},
],
}

Change the env section to your environment. It lints your code whenever you save a file and installs it, which helps gocode to function right.

For going to definition goguru is an amazing tool. I have made a pull request but the PR is not merged yet, you can clone my repo to here:

$ git clone git@github.com:arsham/GoGuru.git ~/.config/sublime-text-3/Packages/

With my latest commit, you can use your mouse or a key shortcut to go to definitions, which is super awesome!

In “~/.config/sublime-text-3/Packages/Default (Linux).sublime-keymap” file I have:

{ "keys": ["ctrl+.", "ctrl+g"], "command": "go_guru_goto_definition", "args": {"output": false}, "context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }] },

With this setup, when I hist ctrl+. ctrl+g it will jump to the definition quickly. The mouse settings are:

{"button": "button2", "modifiers": ["ctrl"], "press_command": "drag_select", "command": "go_guru", "args": {"mode": "definition", "output": false }}

I use ctrl+button1 which is left click for multi-select thingy, and with right click I can travel to the definition.

For refactoring you can install gorename from package control. The settings are sensible, just move the cursor over the thing you want to rename and hit ctrl+alt+r , then hist esc to dismiss the dialog and enter the new name.

While you write codes, you might see some dots in the gutter, they indicate there is a linter message writter in the status bar. Head over to my other article for all the linters I use and how to use them.

Enjoy!