Skip to content

Instantly share code, notes, and snippets.

@shamil
Last active February 5, 2016 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shamil/7268b147c0640ba8045a to your computer and use it in GitHub Desktop.
Save shamil/7268b147c0640ba8045a to your computer and use it in GitHub Desktop.

Using Sublime Text for Go Development

Credits goes to Mark Wolfe.

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

Usually after installing go, I'm running the following commands to make GOPATH available for my user

mkdir -p ~/Documents/My/gocode/src/github.com/shamil

echo 'export GOPATH=$HOME/Documents/My/gocode' >> ~/.bashrc
echo 'export PATH=$PATH:$GOPATH/bin' >> ~/.bashrc 
source ~/.bashrc

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 github.com/golang/lint/golint    

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.

{
    // The maximum amount of memory(MiB) that MarGo is allowed to use
    "margo_oom": 1024,

    // whether or not to indent with tabs (alignment is always done using spaces)
    "fmt_tab_indent": false,

    // the assumed width of the tab character (or number of spaces to indent with)
    "fmt_tab_width": 4,

    // 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/Documents/My/gocode",
        "PATH": "$PATH:$GOPATH/bin"
    },

    "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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment