Supercharge your sublime text for #golang development

Arsham Shirvani
3 min readJan 7, 2017

--

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
$ go get -u github.com/jgautheron/goconst/cmd/goconst
$ go get -u github.com/jgautheron/usedexports
$ go get -u github.com/gordonklaus/ineffassign

I have installed gosublime which is an awesome 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. For goto definition I use GoGuru.

Pay attention to the gutter and status bar when you save your code. This is how your linters communicate with you.

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",
},
"comp_lint_enabled": true,
"ipc_timeout": 2,
"fmt_cmd": ["goimports", "-srcdir", "$_dir"],
"on_save": [
{"cmd": "gs_comp_lint"},
],
"comp_lint_commands": [
{"cmd": ["go", "build", "-i"]},
{"cmd": ["golint $_fn"], "shell": true},
{"cmd": ["go", "vet"]},
{"cmd": ["goconst $_dir"], "shell": true},
{"cmd": ["usedexports $_dir"], "shell": true},
{"cmd": ["ineffassign $_fn"], "shell": true},
],
}

Please note that we are replacing gofmt with goimports here. The first comp_lint_command command (go build -i) makes sure the completion works.

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.

If you hit crtl+.,ctrl+. it shows you the gosublime’s command palette. There are a bunch of functions you can use on daily basis, such as:

ctrl+., ctrl+a: Browse all declarations.
ctrl+., ctrl+p: Manipulate import secion without having to go there.
ctrl+., ctrl+e: Show linter issues.

GoTo Definition

For going to definition goguru is an amazing tool. 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 hit 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.

Refactoring

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.

Generate Tests

For generating tests for your codes, you can use GoTests. This is my user settings for the plugin:

{
"GOPATH": "/home/arsham/Projects/Go",
}

Create a sublime-commands file (~/.config/sublime-text-3/Packages/GoTests.sublime-commands) and put the following:

[
{"caption": "Generate Go Tests", "command": "gotests"},
]

You can use this tool by highlighting a function (or an area inside a function) and hit ctrl+shift+p and choose Generate Go Tests . It will generate the required tests for you in a tests file.

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

Enjoy!

--

--