Jay Taylor's notes

back to listing index

Using Sublime Text for Go Development · Mark Wolfe

[web search]
Original source (www.wolfe.id.au)
Tags: golang go sublime-text sublime-text-3 ide www.wolfe.id.au
Clipped on: 2015-10-12

Image (Asset 1/1) alt=

Mark Wolfe

Random musings and observations

Copyright © 2015 License
Powered by Hugo and Hyde-X

Edit on GitHub

Using Sublime Text for Go Development

Mar 5, 2015 · 3 minute read · 18 Comments

For the last 6 months I have been using Go as my primary development language and for a large part of that I have been using sublime text 3. Along the way the go developers have released quite a few handy and time saving tools which have all been supported by GoSublime with some assembly required. This post will provide a rundown on how to setup go-sublime and the array of tools which make golang development as productive as possible.

So firstly if you’re new to golang then before you start setup your workspace, firstly watch this video Writing, building, installing, and testing Go code.

When I am setting a new system up I typically run the following commands in OSX or Linux, this example is of course for bash, if you use zsh I am sure you can adapt this where needed.

echo 'export GOPATH=$HOME/Code/go' >> ~/.bash_profile
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bash_profile 
source ~/.bash_profile
mkdir -p ~/Code/go/src/github.com/wolfeidau
cd !$

Note: If you’re on OSX you should use .bash_profile, on Linux you typically use your .bashrc.

Once i have done this I can either clone a project from my github or make a directory for a new one.

Now that you have setup your environment setup you can install some tools.

go get -u golang.org/x/tools/cmd/goimports
go get -u golang.org/x/tools/cmd/vet
go get -u golang.org/x/tools/cmd/oracle
go get -u golang.org/x/tools/cmd/godoc

Then install package control in your Sublime editor and add the following plugins.

Then using update your GoSublime user configuration by opening Preferences -> Package Settings -> GoSublime -> Settings User which should open your GoSublime.sublime-settings file, below is the contents of mine.

{

	// you may set specific environment variables here
	// e.g "env": { "PATH": "$HOME/go/bin:$PATH" }
	// in values, $PATH and ${PATH} are replaced with
	// the corresponding environment(PATH) variable, if it exists.
	"env": {"GOPATH": "$HOME/Code/go", "PATH": "$GOPATH/bin:$PATH" },

  "fmt_cmd": ["goimports"],

	// enable comp-lint, this will effectively disable the live linter
	"comp_lint_enabled": true,

	// list of commands to run
	"comp_lint_commands": [
	    // run `golint` on all files in the package
	    // "shell":true is required in order to run the command through your shell (to expand `*.go`)
	    // also see: the documentation for the `shell` setting in the default settings file ctrl+dot,ctrl+4
	    {"cmd": ["golint *.go"], "shell": true},

	    // run go vet on the package
	    {"cmd": ["go", "vet"]},

	    // run `go install` on the package. GOBIN is set,
	    // so `main` packages shouldn't result in the installation of a binary
	    {"cmd": ["go", "install"]}
	],

	"on_save": [
	    // run comp-lint when you save,
	    // naturally, you can also bind this command `gs_comp_lint`
	    // to a key binding if you want
	    {"cmd": "gs_comp_lint"}
	]
}

Note: Ensure you update the GOPATH value to match the one configured earlier.

Once you restart sublime you should be ready to roll!

In addition to these plugins I also use GitGutter which provides some highlighting of changes for source code under git.

Lastly just another tip to enable spell check in markdown files.

Open any markdown file then go to Preferences -> Settings - More -> Syntax Specific - User which should open your Markdown.sublime-settings file, below is the contents of mine.

{
  "spell_check": true
}

Thanks to Marius Ursache for his feedback.