This should workin pretty much any version of Sublime Text, but I have only tested it in Sublime Text 3.
I no longer use Sublime Text in my day to day Go development, and I instead use Atom. I’m hoping to eventually come back to Sublime Text, and I have toyed around with building a Go plugin for ST3, but as of Jan, 2018 Atom simply has better Go support with the go-plus
plugin.
First off, install goimports in your terminal:
go get golang.org/x/tools/cmd/goimports
Next we need to install GoSublime
in Sublime Text. Hit cmd + shift + p
then type Install Package
and select the install package option, then type GoSublime
and select this package.
You may already have the package. An easy way to check is to hit cmd + shift + p
and type Remove Package
then look for it, and hit esc
if you see it.
Next up we need to set up GoSublime to use goimports. To do this, you first want to open up your user preferences for GoSublime. You can do this via the menu (Sublime Text > Package Settings > GoSublime > Settings - User) or via the shortcut (cmd + .
then cmd + 5
).
User settings, not default settings
Make sure you use the user settings. Default settings can be overwritten with a package update, but user settings will be preserved.
Once this file is opened up, you need to add the line "fmt_cmd": [ "goimports" ]
to the file. If your file has nothing else in it, the final version should look like this:
{
"fmt_cmd": [ "goimports" ]
}
If you have something else in it, simply add this line to the file and be sure to add a comma on the line before it if you need to.
Go open up a go file, add some unnecessary packages, and hit save (cmd + s
). The imports should be cleaned up!
Sign up for my mailing list and I'll send you a FREE sample from my course - Web Development with Go. The sample includes 19 screencasts and the first few chapters from the book.
You will also receive emails from me about Go coding techniques, upcoming courses (including FREE ones), and course discounts.
Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.
Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.
Related articles
Spread the word
Did you find this page helpful? Let others know about it!
Sharing helps me continue to create both free and premium Go resources.
Want to discuss the article?
See something that is wrong, think this article could be improved, or just want to say thanks? I'd love to hear what you have to say!
You can reach me via email or via twitter.
©2024 Jonathan Calhoun. All rights reserved.