Setting Atom for Golang on Windows

Golang is another programming language I recently become addicted to. I am a C programmer, and Golang is very similar to it but offers more high-level language’s functionalities e.g. built-in complex structs (slice, map), OOP (kind of) and concurrency (goroutine, channel). This shortens development time like one using Python yet has better performance.

I just bought a laptop, and it is pre-installed with Windows. I’m waiting for Ubuntu 16.04 to come out. That means I have to be nice with Windows for the time being. I enrolled a Golang course on Pluralsight. It teaches how to setup an environment using Atom with Gulp.js on Windows. Gulp.js is used to look for changes made under a working project’s directory. Any change made triggers gocode (autocompletion server) to update its database. It works nicely in their videos, but too bad that I couldn’t do it. So, I looked up the internet and found a way to ditch Gulp.js while functioning the same. And here, this article tells how I get along with Go on Atom on Windows.

Setup Atom Editor

  1. Get Atom editor – It’s free!
  2. Get plugins
    • go-plus – adds extra Atom functionality for the go language
    • atom-terminal – open a command prompt in the current file’s directory (`alt-shift-t`)
  3. Create a project root directory e.g. `C:\Users\pipat\Desktop\hello-go`
  4. Configure go-plus’s `%GOPATH%` environment variable to the project root directory
  5. Get Golang tools
    • From the menu bar `Packages > Go Plus > Get Missing Tools`
      • This downloads source code and install it under `%GOPATH%`

This slideshow requires JavaScript.

  1. Configure gocode to watch for changes
    1. Open a cmd (`alt-shift-t`)
    2. Navigate to your `%GOPATH%` directory
    3. Enter `bin\gocode.exe set autobuild true`

gocode-set-autobuild

After these steps, your Atom has Golang autocompletion and linting features. Be sure to change GOPATH in go-plus every time you switch to another working directory.

Run a Go program in CMD

Here I have a simple Go program printing a value stored in a different package.

go-sample-program

Then, let’s run this program.

  1. Open a cmd (`alt-shift-t`)
  2. Navigate to `%GOPATH%` directory (project’s root)
  3. Enter `set GOPATH=%cd%` (`%cd%` represents current directory)
  4. Enter `go run src\main\hello.go`

This slideshow requires JavaScript.

It’s very important to set %GOPATH% to your working directory every time. Otherwise, Go would not know where to search for packages.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s