Setting up GoLang, Sublime text on Windows

Another great language to add to your full-stack developer tool belt is the simple and productive general-purpose programming language of Go.

Go Lnag With SubLime
Go Lnag With SubLime Text

So let’s get started on the Microsoft Windows operating system. You’ll see just how easy this really is — only a basic working knowledge of GitHub and the command prompt is required. Sure there are other ways of installing and running the program, but with limited coding background I felt this set of instructions was the easiest to understand and follow.

Phase 1: Install the following in this order.

  • As Go uses open-source (FREE!) repositories often, be sure to install the Git package here first.
    Navigate to the Go installation website here.
  • Download and install the latest 64-bit Go set for Microsoft Windows OS.
  • Follow the instructions on the Go installation program.
  • Run the Command Prompt on your computer by searching for “cmd”. Open the command line and type: “go version”

The output after entering go version should look like this:

Go Version Check
Check Go Version

Phase 2: Creating your Go work-space

Create your Go work-space. This will be in a separate and new folder from where the Go installation files are saved. For example, your G installation files were saved under the path C:\Go and you are creating your Go work-space under C:\Projects\Go

Phase 3: Create the GOPATH environment variable

Go to your Control Panel and navigate to System and then Environmental Variables. Then under System Variables click on New.

Next to Variable Name, enter “GOPATH,” and next to Variable Value enter “C:\Projects\Go”

To check that your path has been set correctly, enter “echo %GOPATH%” on the command line.

Phase 4: Test and ensure

Now you’re ready to verify that all is working correctly by opening the command line and typing:

 go get github.com/golang/example/hello

Wait for the code to be entirely implemented (this could take a few seconds), then enter in the following in the command line:

 %GOPATH%/bin/hello

If the installation was successful, you should get the following return message:

“Hello, Go examples!”

Now we are good with Go Lang Setup, lets install Sublime Text 3 now. ( You can skip this if it is already installed)

In the latest version of Sublime Text the easiest way to do that by going to

Tools > Install Package Control….

Tooling integration

The official sublime-build plugin provides integrations so you can execute common gocommands (like go rungo test and go get) without leaving your editor.

You can install it like so:

  1. Open the Sublime Text command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type Golang Build and hit Enter to install the package.

After installation should see a bunch of new tools in your command palette. Their names are pretty self explanatory:

  • Build With: Go
  • Build With: Go - Clean
  • Build With: Go - Install
  • Build With: Go - Run
  • Build With: Go - Test
  • Build With: Go - Cross-Compile
  • Go: Get
  • Go: Open Terminal

Open Existing Hello World Project, previously we have download from GIT.

  • Go to File and click on Open Folder and then give path “C:\Projects\Go”
  • then you can see project directory has two main folders bin and src, now expand src folder and open hello.go file.
  • Click on Command palate and type go run
  • Console shows you output like below.

Environment:
> GOROOT=C:\Go\
> GOPATH=D:\Projects\Go
> Directory: D:\Projects\Go\src\github.com\golang\example\hello
> Command: C:\Go\bin\go.exe run -v D:\Projects\Go\src\github.com\golang\example\hello\hello.go
> Output:
command-line-arguments
Hello, Go examples!
> Elapsed: 1.978s
> Result: Success

Here we done with basic setup, you can start working but we need additional features for that follow below steps.

Automatic formatting

For automatic formatting of .go files I’ve been using the Gofmt plugin. You can install it as follows:

  1. Open the Sublime Text command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type Gofmt and hit Enter to install the package.

By default this will run go fmt -s -e on the current file each time it is saved.

I’ve customised this further to use the goimports tool. If you’re not already familiar with goimports, it runs go fmt and fixes your import lines — adding missing packages and removing unreferenced ones as necessary.

To set this up you’ll need to install goimports and make sure it’s available on your system path:

When that’s installed, you’ll then need to change the Gofmt plugin settings in Sublime Text by opening Preferences > Package Settings > Gofmt > Settings - User and adding the following configuration settings:

{
  "cmds": [
    ["goimports"]
  ],
  "format_on_save": true
}

Restart Sublime Text…
Each time you now save a .go file, you’ll find that it gets automatically formatted and the import packages are updated. No more "imported and not used" errors!

Code linting

For linting of source code I’m using the SublimeLinter plugin. This plugin isn’t a linter itself, but provides a framework for running linters and displaying error messages. You can install it like so:

  1. Open command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type SublimeLinter and hit Enter to install the package.

The next step is to install an actual linter. I’m using gometalinter, which acts as a wrapper around a bunch of different linters and picks up more potential problems and inefficiencies than using go vet and golint alone. You can install it with the commands:

On Command Prompt run below command.

go get github.com/alecthomas/gometalinter

Once that's done, you'll need to install the SublimeLinter-contrib-gometalinter plugin. This acts as the bridge between SublimeLinter and gometalinter.
  1. Open command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type SublimeLinter-contrib-gometalinter and hit Enter to install the package.

By default the linter will run in the background as you type, and errors will be shown in the Sublime Text status bar at the bottom of the screen. But I’ve found suits me more to only lint when saving a file and to display all errors at once in a panel. If you want to do the same, go to Preferences > Package Settings > SublimeLinter > Settings and add the following settings to the SublimeLinter Settings - User file:

{
    "show_panel_on_save": "window",
    "lint_mode": "save",
}

Autocompletion

For autocompletion I’m using the gocode package, which provides a deamon for code completion. You can install it like so:

run below command on command prompt.

go get github.com/mdempsky/gocode

There isn’t currently a gocode plugin available via Sublime Text package control (I might add one soon!)… but there is a plugin included in the subl3 directory within the gocode source itself.

You should be able to copy it into your Sublime Text Packages directory with the following command:

copy $GOPATH/src/github.com/mdempsky/gocode/subl3 ~/.config/sublime-text-3/Packages/gocode

If you open the command palette and run Package Control: List Packages you should see a gocode entry in the list.
By default Sublime Text will make autocomplete suggestions whenever a letter is pressed. But when working with Go I like also to display potential method names whenever I hit the .character. You can make that happen by going to Preferences > Settings and adding a new trigger in the Preferences.sublime-settings - User file:

{

    "auto_complete_triggers": [
        {"selector": "text.html", "characters": "<"},
        {"selector": "source.go", "characters": "."}
    ],
}

You’ll need to then restart Sublime Text for the settings to take effect. Once you have, you should have autocomplete working nicely and looking something like this:

Code navigation

To help with navigating code I use the guru tool, which you can install with the following command:

go get golang.org/x/tools/cmd/guru
where guru
D:\Projects\Go\src\golang.org\x\tools\cmd\guru

To integrate this with Sublime Text you’ll also need to install the GoGuru plugin like so:

  1. Open command palette by pressing Ctrl+Shift+P.
  2. Run the Package Control: Install Package command.
  3. Type GoGuru and hit Enter to install the package.

To use the GoGuru tool, first place your cursor over the piece of code you’re interested in. Then if you open the command palette and type the GoGuru prefix you’ll see a list of available commands, including:

  • GoGuru: callees – Show possible targets of selected function call
  • GoGuru: callers – Show possible callers of selected function
  • GoGuru: callstack – Show path from callgraph root to selected function
  • GoGuru: definition – Show declaration of selected identifier
  • GoGuru: describe – Describe selected syntax: definition, methods, etc
  • GoGuru: freevars – Show free variables of selection
  • GoGuru: implements – Show ‘implements’ relation for selected type or method
  • GoGuru: jump to definition – Open the file at the declaration of selected identifier
  • GoGuru: peers – Show send/receive corresponding to selected channel op
  • GoGuru: pointsto – Show variables the selected pointer may point to
  • GoGuru: referrers – Show all refs to thing denoted by selected identifier
  • GoGuru: what – Show basic information about the selected syntax node
  • GoGuru: whicherrs – Show possible values of the selected error variable

Here we have done our Go Sublime setup next post i will show you how to create new project step by step.

About dwetl

Leave a Reply

Your email address will not be published. Required fields are marked *