Integrating goimports with GoSublime on Sublime Text 3

Brad Fitzpatrick has created goimportsa tool that automagically manages the import statements in your Go code.

You can install it using go get, as usual.

go get

For instance you can write something like:

Loading ...

and goimports will automatically add the missing import statements:

Loading ...

Did you know this works on the Go Playground? Check this code and click on Format.

Installing GoSublime-next on Sublime Text 2 is easy.
  1. Install Sublime Package Manager
  2. Add to the Package repositories
    To do that run Package Control: Add Repository from the Command Palette
  3. Install GoSublime-next using the Package Control: Install Package from the Command Palette
Now we have to make sure that goimports is called every time we save a file.

Open the GoSublime-next default settings file:
- through the menu: Sublime Text > Preferences > Package Settings > GoSublime > Settings - default
- using shortcut: ⌘. ⌘4

Is that file empty? Of course, Sublime Text 3 is trying to open the wrong path.
To solve that, the easiest way is to add a soft link in your Packages directory.

cd ~/Library/Application Support/Sublime Text 3/Packages
ln -s GoSublime-next GoSublime

Now opening the settings file will succeed. The only thing left is to set the "fmt_cmd" option to ["goimports"], and you're done!

Now write some simple Go code without any import statements and save it.
The import statements should appear automatically!