Setup Golang Dev Env in Sublime Text3

Recently I found I like Sublime Text more because of happy coding flow in Golang here.

Very much!!!
As usual, picture first!

1. Setup Go Env

  1. Install Go Env.
  2. Make sure you have GOROOT and GOPATH well-setup.
  3. You should know basic ideas of go package mgmt, like:
    • {src,pkg,bin} under $GOPATH
    • go get to download
    • go build to build
    • go install to install
    • etc …

2. Setup Sublime Text 3

  1. Install Sublime Text3
  2. Install Sublime Text3 Package Control
  3. Install GoSublime Package from Install Package, type gosublime there.

Yes, you’re all done!

3. AutoCompletion in GoSublime

By default, GoSublime will make auto completion when using golang standard library code. If you want to make your own code able to be auto completed, you need to put compiled artifacts under pkg directory of your $GOPATH.

How to do that? Using go get ./...!

3.1. Configuration in GoSublime

Using super+.. (here we means hold super key and double hit dot key), go to User Settings, will open golang user settings config file.

Use the config like below, we use gs_comp_lint when on_save event happens. Of course, in gs_comp_lint, there is a list of commands, in which, we have go get ./... as one of them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
"env": {
"GOPAHT": "/Users/morganwu/Developer/workspace/go",
"GOROOT": "/usr/local/opt/go/libexec"
},
// enable comp-lint, this will effectively disable the live linter
"comp_lint_enabled": true,

// list of commands to run
"comp_lint_commands": [

// run `go get ./...` to install all current project's pkgs
{"cmd": ["go", "get", "./..."]},


// 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"}
]
}

4. Conclusion

  1. Use go get ./... to recursively download and install all packages of current project.
  2. Customize on_save event commands by using User Settings.

Ref:

  1. https://github.com/DisposaBoy/GoSublime/blob/master/articles/golint.md
0%